#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    111
    Rep Power
    4

    Dynamic data to javascript as query string?


    Hi. I'm curious and I'm not sure but I wonder if you can pass data as query string when you define the src on the script tag?

    Something like this?

    src="myscripts.js?id=123"

    Then inside the javascript it can actually use the id data? Is that possible?

    If it's possible do you know how the code will look like?

    Thank you.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,176
    Rep Power
    184
    Yes, you can do this... you would do something like this:

    externalJS.js

    Code:
    var myString = document.getElementsByTagName("script")[0].src.split("id=")[1];
    alert(myString);
    Add this above all of your script tags; so it will be the first script tag, that appears in your document.

    HTML

    Code:
    <script type="text/javascript" src="externalJS.js?id=123"></script>

IMN logo majestic logo threadwatch logo seochat tools logo