...

View Full Version : Command line parameters using <script .. src=script.js?param>



puddleglum
09-04-2010, 03:33 PM
does any expert know how to pass parameters in the <script ..> tag?

for instance;


<script type="text/javascript" src="script.js?param1=val1&param2=val2&etc">


in the javascript script.js, how would we read the params after the question mark?

for example, google this; google shopping cart /v2_2/cart.js

Arty Effem
09-04-2010, 04:54 PM
In the included script file, run an in-line routine that uses document.getElementsByTagName to to read the collection of currently-rendered <script> tags. At that point, its own tag will be the last in the collection, so address that element and read and parse any querystring present in its src property.

gizmo1650
09-04-2010, 05:34 PM
you should also give the variable their own element

<script type="text/javascript" src="script.js" param1="val1" param2="val2">

Arty Effem
09-04-2010, 05:56 PM
you should also give the variable their own element

<script type="text/javascript" src="script.js" param1="val1" param2="val2">If you do it that way the document will not validate.

Dormilich
09-05-2010, 01:39 AM
in the javascript script.js, how would we read the params after the question mark?

out of interest, why would JavaScript need to read those values? I know that I can use a script to prepare JavaScript source and the let the script do the necessary changes.

e.g.

<script type="text/javascript" src="script.php?param1=val1&param2=val2&etc">

qwertyuiop
09-05-2010, 02:38 AM
Someone posted a little script (http://www.codingforums.com/showthread.php?t=202890) that does this. Looks like what you need.

rnd me
09-05-2010, 10:02 PM
In the included script file, run an in-line routine that uses document.getElementsByTagName to to read the collection of currently-rendered <script> tags. At that point, its own tag will be the last in the collection, so address that element and read and parse any querystring present in its src property.

note that any scripts added using a "dom adder", or those using a defer attrib, will not always be the last script in the collection. If you are using those techniques, you'll have to match the .src to be sure you've found the right tag.


--------------
the linked script breaks on "&amp;" and has other issues as well.
here is a true parser that turns GET querystrings into JSObjects:


// turn URL'QS into an object using a parser. takes full urls...
function parseQS(str){
var ob={}, float="", key="", dc=decodeURIComponent;

for( var i=0, mx=str.length; i<mx;i++){
var it=str[i];
if(it==="="){ key=float; float=""; continue;}
if(!it.search(/^[?&]/)){
if(it==="&" && str.slice(i+1,i+5)==="amp;"){ i=(i+4);float+="&"; continue;}
if(key){ob[key]=dc(float);} key=""; float=""; continue;
}
float+=it;
}
ob[key]=dc(float);
return ob;
}



once you find the right script tag, and verify it's .src, feed the entire .src string to the parseQS function, which will return an object containing all the key/value pairs found after the .src's "?".

puddleglum
09-22-2010, 07:23 PM
ok, thanks people, lots of info here.

Google uses gizmo1650's method, and it doesn't validate, and lot of ppl complaining about that...

To answer Dormilich's why?, well it is just very useful to be able to pass parameters, for instance in a footer analytics script, or a shopping trolley, or any script to work across domains, and across multiple pages and contexts, as is our particular need here.

So anyways, Arty Effem tells us the simplest solution..

It is obvious really, but wood and trees and all that....

My simplistic rendition would be;



var x,y,a,b,c,sc=document.getElementsByTagName('script')
for(x in sc) {
a=sc[x].src
if (a&&a.indexOf('script.js')>-1) {
a=(a.substr(a.indexOf('?')+1)).replace(/&amp;/g,'&');
b=a.split(/&/);
for (y in b) {
c=b[y].split('=')
params[c[0]]=c[1] /* for example .. */
}
}
}


Logically same as qwertyuiop's link, not as robust or supremely elegant as rnd me's
but does what we need.

So thanks to everyone, it has been a pleasure......



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum