jquery getting GET variables from javascript file

我与影子孤独终老i 提交于 2020-01-05 08:53:09

问题


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:


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">


来源:https://stackoverflow.com/questions/11279071/jquery-getting-get-variables-from-javascript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!