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 2 of 2
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    Obtain JS Requested Query String

    Hey I need help to obtain the requested javascript's query string.

    Example:
    Code:
    <script type="text/javascript" src="script.js?v=123"></script>
    How can I use that query string inside script.js for example like
    Code:
    var v = 'The code to get the query fragment';
    alert(v); // alerts '123'
    Thank you.
    - DJCMBear
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by DJCMBear View Post
    Hey I need help to obtain the requested javascript's query string.

    Example:
    Code:
    <script type="text/javascript" src="script.js?v=123"></script>
    How can I use that query string inside script.js for example like
    Code:
    var v = 'The code to get the query fragment';
    alert(v); // alerts '123'
    Thank you.
    - DJCMBear
    Code:
    var v = window.location.search;
    v = v.replace(/^\?v=(\d+)/,"$1");
    alert(v);
    http://www.javascriptkit.com/jsref/location.shtml

    Edit: if you have many parametres separated by & you can use split to get a array

    best regards


  •  

    Posting Permissions

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