Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Command line parameters using <script .. src=script.js?param>

    does any expert know how to pass parameters in the <script ..> tag?

    for instance;
    Code:
    <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
    Last edited by puddleglum; 09-04-2010 at 02:39 PM. Reason: more precise example

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    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.

  • Users who have thanked Arty Effem for this post:

    puddleglum (09-22-2010)

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    you should also give the variable their own element
    Code:
    <script type="text/javascript" src="script.js" param1="val1" param2="val2">

  • #4
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by gizmo1650 View Post
    you should also give the variable their own element
    Code:
    <script type="text/javascript" src="script.js" param1="val1" param2="val2">
    If you do it that way the document will not validate.

  • Users who have thanked Arty Effem for this post:

    puddleglum (09-22-2010)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by puddleglum View Post
    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.
    PHP Code:
    <script type="text/javascript" src="script.php?param1=val1&param2=val2&etc"
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Someone posted a little script that does this. Looks like what you need.
    Running Windows 7 x64

  • Users who have thanked qwertyuiop for this post:

    puddleglum (09-22-2010)

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Arty Effem View Post
    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:

    Code:
    // 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 "?".
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    puddleglum (09-22-2010)

  • #8
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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;

    Code:
    	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......


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •