1

I have a link, and when a user clicks it, it pushes a javascript file onto the page. in addition to that, it adds on parameters to the javascript src example:

<script type="text/javascript" src="javascripfile.js?r=some%values&u=more&values">

This isn't getting passed through URL on browser. I was wondering how I would get these parameters inside javascriptfile.js?

javascriptfile.js:

(function(){
     //how to get r value, u value?
}());

Thanks for your help!

1 Answer 1

6

Please try this: http://jsfiddle.net/9Mvxb/ or http://jsfiddle.net/mKNPT/1/ And http://jsfiddle.net/mKNPT/3/

Please note if you are not comfortable using class see this Demo: http://jsfiddle.net/24UdH/

API: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf

further in case you wondering about val.indexOf("?") + 1 why +1 it is to avid ?

This should help, :)

code

$("script").each(function(){
   if ($(this).prop('src').split("?")[0].indexOf("javascripfile.js") )
          value = $(this).prop("src");
});

var returnStr = value.substr(value.indexOf("?") +1);

alert(" value after ? is ==> " + returnStr);

var spliMe = returnStr.split('&');

alert("value of R ==> " + spliMe[0]);

OR

    var value = $(".foo").prop("src");

    var returnStr = value.substr(value.indexOf("?") +1);

    alert(" value after ? is ==> " + returnStr);

    var spliMe = returnStr.split('&');

​

tag

<script type="text/javascript" class="foo" src="javascripfile.js?r=some%values&u=more&values">
Sign up to request clarification or add additional context in comments.

3 Comments

@undefined Bruver Howz life :) Thank-you I shall return this awesomenss lemme see your answer somewhere :)
This answer seems to work great so far... but it echo's out r=, u= as well. I'm just trying to get the values not the r= and u=. Do I have to call these out in the script to remove it? Or is there another faster way? Thanks for the great examples!!
@andrewliu Glad it helped you! yes you got it right! or I made small demo for you to get rid of r= see here: jsfiddle.net/24UdH/2 , have a nice one,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.