SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Calling JavaScript w/ Query String

    I've seen this done a few times and just wondered what the purpose was:

    HTML Code:
    <script src="scripts.js?1234"></script>
    Is JS capable of viewing the query string that was used in its own src attribute? Or is that "1234" doing something else?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Location
    Kingdom of Saudi Arabia
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably to prevent caching on the browser especially if the JS contains a dynamic JSON or similar data...especially IE which sometimes yields an unexpected if values get was from a cache JS.

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    I don't think you can access the query parameters on the javascript's own src attribute, unless you were to loop through all the <script> tags and analyse the src attributes.

    Like modtup says, it's usually used to influence caching in the browser.
    Another use is that if the file is parsed by a server-side language (such as PHP), then the parameter will be available in $_GET.

    Code javascript:
    <script src="scripts.php?val=1234"></script>

    scripts.php:
    Code php:
    $value = $_GET['val'];


Bookmarks

Posting Permissions

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