John O John O - 1 month ago 10
Javascript Question

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

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.

Answer
//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];
Comments