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

2019-07-13 02:34发布

Is it possible to have a javascript link like this (notice the query string):

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

and then in myScript.js get the value of v using jQuery? A javascript answer is okay too, but jQuery is preferred. Thanks in advance! I searched all over and all I could find were questions pertaining to getting the value of a query string in the URL, but not from it's own link.

2条回答
叛逆
2楼-- · 2019-07-13 03:05

There is no reason for you to pass information through the link when calling a script. That JavaScript will run after it has been called. There is for sure a better way to do what you are trying to acomplish with this.

Something like this might give you an idea.

 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.
}
});
查看更多
萌系小妹纸
3楼-- · 2019-07-13 03:06
//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];
查看更多
登录 后发表回答