[英]jquery getting GET variables from javascript file
I have a link, and when a user clicks it, it pushes a javascript file onto the page. 我有一个链接,当用户单击它时,它会将javascript文件推送到页面上。 in addition to that, it adds on parameters to the javascript src example:
除此之外,它还在javascript src示例中添加了参数:
<script type="text/javascript" src="javascripfile.js?r=some%values&u=more&values">
This isn't getting passed through URL on browser. 这不会通过浏览器上的URL传递。 I was wondering how I would get these parameters inside javascriptfile.js?
我想知道如何在javascriptfile.js中获取这些参数?
javascriptfile.js : javascriptfile.js :
(function(){
//how to get r value, u value?
}());
Thanks for your help! 谢谢你的帮助!
Please try this: http://jsfiddle.net/9Mvxb/ or http://jsfiddle.net/mKNPT/1/ And http://jsfiddle.net/mKNPT/3/ 请尝试以下操作: http : //jsfiddle.net/9Mvxb/ 或 http://jsfiddle.net/mKNPT/1/ 和 http://jsfiddle.net/mKNPT/3/
Please note if you are not comfortable using class
see this Demo: http://jsfiddle.net/24UdH/ 请注意,如果您不喜欢
class
请参见此演示: http : //jsfiddle.net/24UdH/
API: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf 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 ?
如果您想知道
val.indexOf("?") + 1
为什么还要+1
来狂热?
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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.