Adding variables into a javascript .js file?

Discussion in 'JavaScript' started by Seqqa, Oct 8, 2008.

  1. #1
    Can I add values into a Javascript file like the example below?

    /javascript.js?a=whatever&b=whatever

    Then get them with '+a+' and '+b+'

    Is it ok to do that?

    Thanks for your help!
     
    Seqqa, Oct 8, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    no. you cant.

    but you can add variables to a php script. rename your js to .php, then have:
    /javascript.php?a=whatever&b=whatever

    and within the file, just iterate $_GET or do absolute varnames like

    var a = '<?=$_GET['a']?>', b = '<?=$_GET['b']?>;

    hth
     
    dimitar christoff, Oct 8, 2008 IP
    Seqqa likes this.
  3. Seqqa

    Seqqa Well-Known Member

    Messages:
    3,695
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    115
    #3
    That's a good idea, thanks!

    +rep
     
    Seqqa, Oct 8, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well technically you can, just loop through the script elements and check the src.
     
    MMJ, Oct 8, 2008 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    ah cool idea also.
     
    dimitar christoff, Oct 8, 2008 IP
  6. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Actually, you can.
    function gup( name )
    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return results[1];
    }
    Code (markup):
    The function above will return the value of an URL parameter.
    Usage example:
    gup('hello'); // This returns 'world' from http://domain/page.php?hello=world
    Code (markup):
    taken from: http://www.netlobo.com/url_query_string_javascript.html
     
    djzmo, Oct 8, 2008 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    this is not correct- it will get parameters to the page that EMBEDS the js, not params to the js itself. different problem altogether.
     
    dimitar christoff, Oct 9, 2008 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Awful code, I hate it when people use regular expressions instead of parsing a string which is usually much easier. Also you have to call decodeURIComponent on the string.
     
    MMJ, Oct 9, 2008 IP