jQuery从它自己的(self)链接中获取查询字符串

jQuery get query string from it's own (self) link

本文关键字:链接 获取 查询 字符串 self 它自己 自己的 jQuery      更新时间:2023-09-26

是否可以有这样一个javascript链接(注意查询字符串):

  <script type = "text/javascript" src = "/js/myScipt.js?v=3"></script>

,然后在myScript.js得到v使用jQuery的值?javascript的答案也可以,但jQuery是首选。提前感谢!我到处搜索,我所能找到的都是关于在URL中获取查询字符串值的问题,而不是从它自己的链接。

//to set script do some thing like this
var version = 3;
document.write('<script type = "text/javascript" src = "/js/myScipt.js?v='+version+'"></script>');
//or 
$("body").append('<script type = "text/javascript" src = "/js/myScipt.js?v='+version+'"></script>');

//to get v from myscript.js
var getV = document.currentScript.src.split("?v=")[1];
// var getV =  $('script').last().attr("src").split("?v=")[1];

在调用脚本时没有理由通过链接传递信息。JavaScript将在调用后运行。肯定有更好的方法来完成你想要完成的事情。

这样的东西可能会给你一个想法。

 var value;
$("script").each(function(){
var temp = "v=";
If(this.src.indexOf(temp) > -1 )) > -1){
get last digits before "&" if any exist.
And after the index of temp var.
}
});