Help - Search - Members - Calendar
Full Version: Can't Get Msgbox To Work
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > JavaScript
ultimategamer50
I don't know if this belongs in Javascript or PHP but this does have PHP in it. Ok I can't get this javascript delay msgbox to work in PHP, what am I doing wrong in this code?
CODE
echo "<script type=text/javascript>
<!--
setTimeout(alert('testing!!');,3000);
window.location=http://google.com);
//-->
</script>";


It works fine in HTML
swordz
This is javascript, there's no PHP in this block of code.

Try:
CODE
echo "<script type=text/javascript>
<!--
setTimeout("alert('testing!!');",3000);
window.location=http://google.com);
//-->
</script>";


Swordz
ultimategamer50
QUOTE(swordz @ Sep 17 2009, 10:43 AM) *
This is javascript, there's no PHP in this block of code.

Try:
CODE
echo "&lt;script type=text/javascript>
<!--
setTimeout("alert('testing!!');",3000);
window.location=http://google.com);
//-->
</script>";


Swordz

actually it's javascript in a PHP file. why won't it work?
Straystudio
There are more errors, and JavaScript gets blocked with little.

The first part to work:
setTimeout wants the instruction going to be executed, to be wrapped in quotes, single or double as well as alert requires for its string-message; so, the correction by swordz would be enough, if JS/HTML alone.
He alternates ' and " quotes, they not to interfere: You can't do setTimeout('alert('testing!!');',3000); nor with repeating " (JS gets blocked!); this is right, as for JS:

setTimeout("alert('testing!!');",3000);

BUT, since PHP echo introduces string by quotes itself, interference would occur again.
We can use backslash as escape character in place; this works:

CODE
<?php

echo "&lt;script type=text/javascript>
<!--
setTimeout(\"alert('testing!!');\", 3000);

//-->
</script>";

?>

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.