View Full Version : Passing URL param to Javascript

07-17-2012, 11:13 AM
Hi, I'm sure this is a simple one, but I can't find a simple answer anywhere.

I have a webpage that calls a javascript calendar and one of the parms passed is how many months to display. What I want to do is pass a parm on the URL to set the number of months to display. I cant figure out how to pass the parm value to the parm value needed by javascript. e.g.

[ICODE]<script language="javascript" src="/calendar/load.php?view=3&cid=1"></script>[ICODE]

view = number of months to display.

I want to be able to have ?view=myparmvaluefromURL

I cant get the page to resolve the value for myparmvaluefromURL and pass to ?view parm.

Any help appreciated. At the moment I just have multiple pages with the values for 3,6,9 and 12 hardcoded - but means I have 4 pages to maintain when updating.


07-17-2012, 02:25 PM
You can use document.URL to get the URL, then split the URL several times.

First split on ? which will give you the URL as position 0, then the params as position 1 of the array (or the object that can be treated like an array.) Then split on & to get each name/value pair, then split each on = to get the name and value.

Old Pedant
07-18-2012, 12:09 AM
Ummm...I could be wrong, but I *THINK* that what he means is that he wants the PHP page to receive those parameters, AS IS:

But then he wants the JAVASCRIPT as generated by the PHP code to see a *DIFFERENT* set of parameters.

There's no simple way to do that.


But it's trivial to pass a value from PHP to the JavaScript of a page:

<script type="text/javascript">
var anyName = "<?php echo $somePhpVariable; ?>";

So WHY would you want to try to get the parameters from the URL when you can pass stuff in from PHP code so easily?

If I have completely misunderstood, maybe an example from macleda would help?

07-18-2012, 09:19 AM
Hi Old Pedant

I think maybe I didn't explain properly. It is PHP script that should receive the variable (hopefully from HTML parms when I call the page). I have no access to the PHP as it's 3rd party calendar protected by ZEND.

If you look at my live page you will see how I am handling option to display 3,6,9 or 12 month calendar.


What I can't figure is how to get HTML param value resolved into the variable passed to the PHP.

07-18-2012, 09:21 AM
Thanks Wolfshade, unfortunately, I don't have access to the PHP that uses the parameter passed, so need to be able to pass from the calling page - if possible.