View Full Version : Passing php to external javascript

11-15-2007, 10:46 AM
Total programming newbie here but I'm doing some experimenting and can't solve this. Not even sure if it's possible, but here goes.

I have a standard html page that calls an external javascript popup window. I want to be able to pass a php variable into the javascript and call it from within the .js file.

ie, <script src=http://www.mydomain.com/script.js?1234></script>

this calls the script.js to bring up the alert window. Now within the alert window, there is an action statement which directs the user to a URL. Now all I want to do is append the "1234" to the end of the url that is defined in the .js script.

ie. script.js will contain the following line to point the visitor to somewhere.
//action = "href='URL'"; // open URL case
action = "HREF='http://www.outside.com/1234'";

Basically I need to be able to parse the ?1234 somehow into the .js and be able to append the variable at the end or embed it within the URL I want the visitor to go to.

hope that makes sense!

Thanx for your help!

11-15-2007, 11:35 AM
Hi there,

You could put some PHP into your parent page thus:

<?php $page = "1234"; ?>
<A href="#" onclick="javascript:window.open('http://www.outside.com/<?php print($page); ?>');">Open the pop-up</A>


<?php $page = "1234"; ?>
<A href="http://www.outside.com/<?php print($page); ?>" target="_blank">Open the pop-up</A>

Here's an explanation that I found at random with a Google search: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

That being said, pop-ups are routinely blocked by web browsers, plus javascript can be turned off in a web browser's settings, and so I tend to write pages so that pop-ups are not necessary (ask yourself the question "how can I do this without javascript and pop-ups?") because end users (and that means anybody who can access the internet - which means anybody!) tend to be confused by pop-ups (my Mother, for example!), also people increasingly associate pop-ups with viruses and adware (in other words they make the mental association "pop-ups = dodgy website"). I for one also prefer to use web pages where everything is clearly presented in a single page without the need for javascript, pop-ups and the like.

I hope that helps!