is it possible to prevent further execution of javascript?
I include some javascript scripts with php in a header (with echo ''), but there are coming some other scripts later in the page which i can not always control, so it could be that my before included (with ) mootools javascript get later overwritten by another included mootools (which then possible is an older version, or is not complete etc.)
so is there a way that I can stop the js at one point so that later js code will not be executed?
kind of like the die(); function in php, but without that it stops the page from being loaded.
doesnt really exist.
but you can put everything into a function and "return;" any given time to exit the function which would stop the execution of the rest of the code within the function.
super simple example in standard JS:
function init() {
if(something happens) {
return;
}
}
init();
in terms of stopping the browser from executing other scripts within the page - not possible.
Related
Run php script, display the response, and start another php script and then display it
I have two functions in php I would like to display the response of the first function in order to make loading less long
Exemple Pseudo-code
Function hello1()
{
echo 'hello1';
[...]
}
Show response and run another function
Function hello2()
{
echo 'hello2'
[...]
}
all php methods will be executed before rendering the html page. May you think about Javascript event handling or an method "injections" with Ajax request to a code behind file which is php based.
Regards,
Tobo
My question is simply if i have a javascript function onload like so
<body onload="myFunction()">
will this function be executed BEFORE the PHP in the rest of the body ?
No.
onload doesn't get executed until the entire document, including images, has been rendered
PHP runs on the server (before the browser receives the file)
It is possible for some of the output of a PHP script to arrive in the browser before the entire script has finished executing, and you could even set things up so that the HTTP response finishes before the script has finished — but that takes some complicated jiggery pokey, is rarely useful, and you should know if you've gone to such lengths.
The function itself explains it "onLoad", so when your page is loaded ( including PHP ), your function then will be executed.
I have a page with some user selectable options and a button that, when clicked, runs a PHP script and then refreshes a div with another PHP file that uses a session variable that is created at the end of the first PHP script. If the user presses the button again, with different options selected, the div is updated using the newly replaced session variable. The problem is that sometimes, perhaps 1 in 10 times or so, the old session variable data is loaded. I suspect that the second PHP file is catching the variable too early, before it has been updated, but I tried unsetting the session variable at various points with out any luck.
First PHP file:
session_start();
$needle = array();
foreach($_POST['checkboxes'] as $key => $value){
$needle[] = "$value";
}
// code that processes the values from needle and outputs $data
unset($_SESSION['data']);
$_SESSION['data']=$data;
Second PHP file:
session_start();
echo $_SESSION['data'];
Javascript:
$(".userdata").click(function() {
$.post("first.php", $("form#checkboxes").serialize());
});
$(function() {
$("#button").click(function() {
$("#div").load('second.php')
})
})
The problem is that in some cases the first PHP script has not finished running before you click the button that loads the second PHP script (like I implied before in my comment). The fact that this happens is related to how scripts are scheduled by the webserver (which is a different subject entirely).
You thus need to make sure that when you click the button that runs the second script, the first script has completely finished running.
Because in my knowledge, javascript does not allow blocking/signaling on a variable (like Java does), you'll have to use a more 'dirty' technique called busy waiting.
The best way to do this, is to include an extra variable in the javascript you are using.
var wait = false;
function reloadSecond (){
if (wait){
setTimeout('reloadSecond()',200);
} else {
$("#div").load('second.php');
}
}
$(".userdata").click(function() {
wait = true;
$.post("first.php", $("form#checkboxes").serialize(), function(){
wait = false;
});
});
$(function() {
$("#button").click(reloadSecond);
})
While 'busy waiting' is generally not considered the most elegant solution, I think you don't have many other options in this case (except for serverside push, which is much more complicated). Additionally, you'll probably only incur the extra 200 millisecond (or less, you can of course change this value) waiting time once or twice.
(side note: I assume that javascript is single threaded here, which is true in almost all cases: Is JavaScript guaranteed to be single-threaded?).
Can it be a case where the browser or proxy server is caching the html data? Try setting the headers to tell them not to cache. See the examples in http://php.net/manual/en/function.header.php for what headers to set.
I the following code:
<? $test = new com("Soundclass.Soundrec"); ?>
<? $test->startrec ?>
<script>
function stop(){
var stop_record = "<?=$test->stoprec;?>";
}
</script>
and I am running the stop function in a button click. But the php function seems to run without the click.
The purpose of this is to stop the recording on that button click.
You have a little logical error:
PHP is server-side and gets executed on the server when the document is called.
Javascript is client-side and gets called dynamically, it can't re-execute any php-code again.
See the source code in your browser and you'll understand!
But you could make an ajax call to the class and execute the stoprec() method with a (defined in your script) get/post variable. But as this still won't give you a handle on the same instance of the object, so unfortunately you probably have to rethink your whole script!
What you are trying to do is fundamentally impossible: You are mixing up PHP (which runs on server side) and JavaScript (which runs on client side).
You would have to build a second PHP script that stops the recording, and have that called from JavaScript using Ajax.
Calling $test->stoprec will probably stop recording (don't know Soundclass.Soundrec that specifically myself).
As you call it even before the page has been delivered, pressing the stop button later won't make a change here.
You need to execute the PHP later (after button click), however you have the general problem here that the com object in $test won't survive that (it would be a new one).
The only solution I see here is that you create a daemon that manages sound recording for you. This would work with AJAX, but is not trivial. So the short (and sad) answer is: Not easily possible.
For example:
$(document).ready(function(){
$('.selector').click(function(){
<?php
// php code goes here
?>
});
});
Will this cause issues or slow down the page? Is this bad practice? Is there anything important that I should know related to this?
Thanks!
If you are trying to bound some PHP code with the click event then this is impossible in the way you are trying and PHP code will be executed as soon as page load without waiting for a click event.
If you are trying to generate final javascript or jquery code using PHP then this is okay.
It won't slow down the page; the PHP runs on the server and emits text which is sent to the browser, as on any PHP page. Is it bad practice? I wouldn't say "bad" necessarily, but not great. It makes for messy code - in the event where I need to do something like this, I usually try to break it up, as in:
<script>
var stuff = <?php print $stuff; ?>;
var blah = "<?php print $blah; ?>";
// Do things in JS with stuff and blah here, no more PHP mixed in
</script>
PHP is executed on the server, and then the javascript will be executed on the client. So what you'd be doing here is using php to generate javascript that will become the function body. If that's what you were trying to do then there's nothing wrong with doing it.
If you thought you were going to invoke some PHP code from javascript, then you're on the wrong track. You'd need to put the PHP code in a separate page and use an ajax request to get the result.
Sure, as long as you keep in mind that PHP code will be executed by the server before the page is sent out. Other than that, have fun.
PHP is a "backend" language and javascript is a "frontend" language. In short, as long as the PHP code is loaded through a web server that understands PHP - the downside is that you have to inline the JS, losing caching ability (there are workarounds to parse php in .js files but you shouldn't really do this). To the user it will just look like javascript and HTML. Here's the server order:
User requests page.
Apache (or equivalent) notices this
is a php file. It then renders all
the php that are between php tags.
Apache sends the page to the user.
User's browser sees the JavaScript
and executes it.
Just be sure the PHP is outputting valid JavaScript.
you have a better choice to use ajax that runs the php script when you are handling a click event
$(document).ready(function(){
$('.selector').click(function(){
$.ajax({url:"phpfile.php",type:"POST",
data:"datastring="+value+"&datastring2="othervalue,
,success:function(data){
//get the result from the php file after it's executed on server
}
});
});
});
No it's not. Just as long as you know that the JS is executed after the PHP page is parsed.