I currently have a button when clicked it should open up a new window but I am getting this error: Uncaught SyntaxError: Unexpected token :. I am using window.open()
$url = "http://www.google.com";
$button .=
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open('.$url.');"/>';
I have noticed that when I use window.open() with no parameters it works but when I pass in the $url variable all hell breaks loose. I get syntax errors. I’ve tried http://www.google.com, http://www.google.com and google.com to no avail!
Thanks for your help in advance!
Because you are missing the single quotes needed to encapsulate the url string.
You’re adding a layer of string encapsulation. When you pass a string value to a function it must be in quotes, as it is a string.
When you are doing this inline in your html, you need to encapsulate the javascript in double quotes so it becomes
Then, if you want PHP to echo that, or to assign it as variable, you need to enclose all of that in quotes, so you can do
Any way you break it down you need have 3 string encapsulations embedded in one another, so you must find a way to differentiate between the quotes used in JS and the quotes used in PHP.
Otherwise you would have a problem.