This function query's a window.location and grabs the variables after ? and & <script> function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } alert('Query Variable ' + variable + ' not found'); } </script> Now make a request to page.html?x=Hello <script> alert( getQueryVariable("x") ); </script> Code (markup): What I'm trying to do... I have a script that requires another script <script src=myscript What I'm trying to do is send some variables from A to work in B (myscript) <script src=myscript.js?something=something&something=something On the script above line 2: var query = window.location.search.substring(1); Can that be edited somehow ? so the variables from the requested .js file end up in B (myscript) ? I'll be placing the (edited) script inside myscript
no. you cannot (to the best of my knowledge) obtain parameters passed in such a way here. window.location.href to the second .js will be the same one available to the first one (url in the browser). i am not sure if you just do vanilla javascript or not but you have several options here. option one: move script 2 into a php/asp file so you can then do <script src="myscript.php?something=something&something=something" type="text/javascript"></script> and then print the js accordingly. option two: why not just set the that script 2 needs to be global? for example, if I had to call up a second script (I use the mootools framework), this would be valid: var myoptions = { something: "something", something2: "something" } new Asset.javascript("/script2.js", { onload: function() { // wait until the script has finished loading before calling any functions defined in it. // do something ... } }); Code (markup): in theory in script2 you should have the myoptions object. w/o mootools, just interact with the DOM and create a new Element 'script' and set src etc, you should be able to find this on google. doing <script src=''> for the second script as a part of your page won't work because it will init the script before the variables needed are getting set from the first one.
Thanks for your time Dimitar, you will have to excuse my ignorance... Yeah I knew about the window.location.search.substring(1); only taking values from the browser... that was basically my question... so the answer to that is no... ok So your options : PHP hmm the only part of that is, the something=something will change something=somethingElse theres no fixed value For example I'm catching a value from the first script called target its displayed with a magic-macro {target} and it can current vary from any letter or number from 1-0 a-z etc... the second something2 = a URL which also vary's from anything really http://....... displayed as {clickurl} So if I call script 2 with <script src="myscript.js?target={target}&clickurl={clickurl}" type="text/javascript"></script> I get on the following page <script src="myscript.js?target=a&clickurl=http://someurl.com" type="text/javascript"></script> So your suggesting I create a new file php and use <script src="myscript.php?target={target}&clickurl={clickurl}" type="text/javascript"></script> placing this code var myoptions = { target: "", clickurl: "" } new Asset.javascript("/script2.js", { onload: function() { //I put my original javascript here } }); Code (markup): Is that it ? how do I write the 'target' and 'clickurl' variables into the javascript, is it +target+ or just 'target' Sorry for what might seem ridiculous questions, I'm a noob, I know... I just don't see how the php file could determine what the target and clickurl equal. Basically script2.js is currently loading from a database, and ideally I want to take it out of there and use a request <script src= to load it... its small in size, but its in the database multiply times, I feel it would be better to just have a request in the database to load the file externally. I can post my current code if you like and that might shed some light on what I'm trying to achieve or if you click my netaudioads signature link and visit the demo page, you will hear a advert that tells listeners to press a keyboard key {target}, then a modified Alert box (Yes using mootools) opens with a link {clickurl} its that shortcut function I want to load externally
not quite. if you take the <script src="myscript.php?target=a&clickurl=http://someurl.com" type="text/javascript"></script> route, then this is what you got to do: treat it as a normal JS but at top of the script do something like: var target = '<?=$_GET['a']?>', clickurl = '<?=$_GET['clickurl']?>'; then any Js function that follows will have target and clickurl available to it as defined in the embed parameters. the solution where you pass on the values via the gobal js scope and load the second script afterwards is probably a bit early for your needs, can't run before you can walk anyway - the php solution will be just fine. good luck!
The above is working out just great, however I'm now in a position that I need to possible create a cookie for other variables, @deathshadow could you possible post a snip it of code that relates to the above as I'll need to send 2 variables in the form of cookies as i cant see any other way of doing what I need. Cheers and thanks for your time...
if you do something like: // ... for argument's sake you have setCookie and getCookie defined already. <script type="text/javascript"> var foo = "bar"; setCookie("foo", foo); </script> <script type=text/javascript" src="script2.js"></script> PHP: and script2.js: var foo = getCookie("foo"); alert(foo); // bar. PHP:
so there after I use $_POST['foo'] & $_POST['foo'] ? using your method above can I $_GET foo : example below <script type="text/javascript"> var foo = "bar"; setCookie("<?=$_GET['foo]?>", foo); </script> <script type=text/javascript" src="script2.js"></script> Code (markup):
is this for passing on a variable from a .js to another .js? this is slightly more complicated - does this describe the hieracy: you'd have a parent .html doc. it embeds 1 js (a .php file which sets some session variable) it embeds a 2nd js (a .js which takes some values from the php .js file) if so, this wont work. you need to make sure script 2 does not get loaded before you have arrived to the correct values and have set the cookie. there was a topic i covered about delaying script loading...