I'm using this code in a php conditional if/else statement to open a new window.
echo("<script type=\"text/javascript\">
<!--
window.open(\"http://www.example.com/index.php?$var\");
//-->
</script>
");
It's opening 2 windows instead of one, but only on my live site (on a dedicated server) and not on the test site (shared server).
EDIT: From zebediah49's suggestion, I appended a random int variable after the new window url. It changes the variable with each new window instance, so I assume that means that it runs the conditional twice. I probably should mention that it's using Joomla and a 3rd-party community app, JomSocial.
Alright, as I was editing this and after ~5min of the same window being open, it opened a brand new instance of the window.open window. So obviously it automatically refreshes once as soon as it lands. I'll have to pry into that. Still any suggestions are welcome.
Thank you for your tips regarding language and best practices.
Since it's performing differently, there is either something different in the code between the two, or something different in the environment.
To test to be very, very, very sure that it is opening that specific url twice, have it append a random variable to the end of the URL:
$rando = rand();
window.open(\"http://www.example.com/index.php?$var&r=$rando\");
Also, if you want to avoid having quote issues, I would advise using HEREDOCs:
echo(<<<EOF<script type="text/javascript">
<!--
window.open("http://www.example.com/index.php?$var");
//-->
</script>
EOF
);
or with a variable
$content = <<<EOF<script type="text/javascript">
<!--
window.open("http://www.example.com/index.php?$var");
//-->
</script>
EOF;
echo($content);
I can't think of a good reason why that single line of JS would happen twice though, which is why I would want to see with the random var, such that there is no possible way other than that it could get opened twice. (If it was somewhere else in the file, it would not have the &r=...; if it was the same thing getting executed twice, &r=... will be different between the two)
First of all.
Why those in your Javascript ?
Those are XML Comment.
You need to use /* ... */ or //
Did you mean
//<![CDATA[
...code...
//]]>
If this is the only call to window.open() your http://www.example.com/index.php might also make another window.open() call
Could it be that the PHP echo is being triggered twice?
Related
I have a link which, when the page is called or refreshed, runs.
As this is set to execute onclick, this should not be happening.
I had a sneaking suspicion that because I had left the path empty in href that it might be causing the issue, but adding in a file location does not change the outcome.
<img src="lightm.png" width="128" height="128">
You are mixing client side and server side code here. All PHP will be evaluated whenever your page is rendered (unless there is a control structure like an if statement to stop it from being).
You can only put JavaScript in an onclick event. If you want to trigger server side code when an element is clicked you need to use AJAX.
EDIT: I would like to give some credit to #Chris as he's very well pointing out a key point in his answer: You are mixing client side and server side code here. He also confirmed the fact about how PHP code would execute no matter what that I refer to as assumptions and guesses below, and have since turned out to be true, of course.
Below is my original answer:
I have no experience with the exec function, but somehow my gut feeling tells me it will execute no matter where it is in a HTML document. Actually, it's like any function with PHP, you don't actually need it to be formatted properly in an HTML file, it will run anyway (well, unless it's commented out), so that's the reason it calls every time, I guess...
Best if you use AJAX instead or some other means to load your external file, not a PHP call (server), considering you're using onclick that returns the click event handler code (client).
Example:
<img src="lightm.png" width="128" height="128">
The AJAX call:
function myFunc() {
$.ajax({
url: "/var/www/html/lightson.py",
// rest of the AJAX magic
});
}
Okay, I have created a new question to clarify my old one, which is available here: Check if certain text was outputted to the screen PHP
Currently I have this code:
<?php
echo "
<noscript><h2>! JavaScript is not enabled!!! Features will not work !</h2></noscript>
<script type=\"text/javascript\">
document.cookie= \"jsEnabled=true\";
</script>
";
if (isset($_COOKIE['jsEnabled'])) {
// Javascript is enabled!
}
else {
die("JavaScript is not enabled!");
}
?>
I am not sure why this wont work! It should kill the PHP if JS is disabled! Thanks!
JavaScript processes after PHP has fully given out the page, not before, and not in symbiosis. As such, your PHP call will only work for the second call to the page, not the first.
That is, if you accept cookies in the first place.
If you want to prevent users without JS from using the interface on a page, consider generating the interface in pure JS instead. More reliable.
Where am I going wrong with my programming logic here?
I have 2 php files. File 1 includes File 2. File 1 calls a php function from File 2. Inside the php function there is a bunch of html. The html works perfectly. At the end of the function I have this javascript....
<script type="text/javascript">
alert('hello');
</script>
This javascript isn't alerting "hello".
What am I doing wrong?
Thank you in advance.
EDIT: New question because I skrewed the last one up.
In theory would the code below run properly? (yes/no)
<?php function AlertHelp(){
?><script>
alert('help');
</script><?
AlertHelp();
?>
Long shot on a wild guess here with the limited information you gave.
My assumption is that you are not "including" the file via PHP's include, require, include_once or require_once functions, but are in fact using AJAX to load in the page's content.
If this is the case, then I shall also assume you're using innerHTML to put the content on the page.
Suddenly the solution is obvious: <script> tags added by innerHTML are not parsed and run. You could probably do something like this:
// assume `result` is the variable containing the AJAX response and `elem` the element it goes in
elem.innerHTML = result; // this doesn't change
result.match(/<script[^>]*>([\s\S]*?)<\/script>/i,function(m) {eval(m[1]);});
Please note however that eval should be avoided if possible. Consider redesigning your layout to use callbacks instead.
I'm trying to replace a bit of javascript in my page via AJAX, but for some reason, AJAX wont replace it...
When I use:
alert(document.getElementById('treintracking').innerHTML);
I can clearly see the javascript from the script piece: (this is the opening line of the javascript piece)
<script type="text/javascript" id="treintracking">
For replacing the script I use this:
document.getElementById('treintracking').innerHTML = responseText;
So, why does AJAX not want to replace the javascript?
I've tested, and the php file used to generate the replacement javascript, works fine.
I also took into account that the to-be-replaced javascript already has tags around it, so I removed those in the php file.
But it still wont replace the content...
Also, if it put somefunction() in that javascript, will it then run, or do I have to do something special?
Note: the javascript script is generated in a php file.
SOLUTION:
I am now using this external solution, I don't have a clue how it works, but it works perfectly:
http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml
[I took the loading script from the page source, as it wasnt in the article itself...]
Adding JavaScript via innerHTML does not get evaluated.
If you want to add new code, just set the source to a new external JavaScript file.
So instead of using an Ajax call, you just set the src
document.getElementById('treintracking').src = "new/path.php?a=b";
Another solution [that I would avoid at all costs] is eval().
You'll probably have to embed the new javascript inside a function, which may assign new contents to other functions, and then invoke the outer function. Won't be terribly pretty.
I have a php script that is a bit of a mess and after a form entry, I need to get an address, and display it on a google map. The html and php is crammed into the same script so I essentially need to call the JavaScript as the PHP is happening. Is there a way to do this?
Thanks,
Alex
You can POST your from to a different frame (or iframe), so your page would not reload. The response of your PHP file which comes back to that frame can contain JavaScript code, which will be executed. Something like:
echo('<script type="text/javascript"> alert("Executed on client side"); </script>');
No, PHP executed by the server and returns the full response to the browser. JavaScript in the page is then executed by the client.
You can't call Javascript functions from PHP. You can set the Javascript to run when the page loads instead.
What you want is something like this:
<script type="text/javascript"></script>
var userAddress = "<?php echo $_POST['address']; ?>";
doSomethingWithAddress(userAddress);
</script>
If that code is on the page which you are POSTing the address to, it would take the address from the user, and write it into a javascript tag. The PHP will get executed first on the server, before building the HTML document. This new document has the variable available to the javascript.
I don't know how you would go about doing that, but this seems like a good place to start looking:
http://code.google.com/intl/en/