Get query data in javascript from src attribute - Stack Overflow

Often, when I update a JavaScript or CSS file used by a webpage, I will tack on v=<version>. This

Often, when I update a JavaScript or CSS file used by a webpage, I will tack on v=<version>. This helps me track what changes I have made but also forces the browser to get the newest version instead of using cache. Example:

<script src="functions.js?v=1.4"></script>

Is is possible, using JavaScript, to detect any query parameters used? Meaning, could function.js detect that v is 1.4?

Often, when I update a JavaScript or CSS file used by a webpage, I will tack on v=<version>. This helps me track what changes I have made but also forces the browser to get the newest version instead of using cache. Example:

<script src="functions.js?v=1.4"></script>

Is is possible, using JavaScript, to detect any query parameters used? Meaning, could function.js detect that v is 1.4?

Share asked Feb 28, 2014 at 20:09 steveo225steveo225 11.9k16 gold badges69 silver badges120 bronze badges 4
  • Well you could parse the version out of the href – Sterling Archer Commented Feb 28, 2014 at 20:11
  • This is a duplicate of stackoverflow./questions/7992354/… Look at this page to get the answer. – Pascal Le Merrer Commented Feb 28, 2014 at 20:15
  • It's duplicate question: stackoverflow./questions/2976651/… – EAndreyF Commented Feb 28, 2014 at 20:16
  • @EAndreyF, not exactly. If the question is about can the script get its own query params, it's not. – dentuzhik Commented Feb 28, 2014 at 20:20
Add a ment  | 

2 Answers 2

Reset to default 4

There's no a specific method for this, but you can inspect the DOM as usual:

var script = document.querySelector('script[src~="function.js"]');
script.src.replace(/.+?v=/, ''); // that's your version
<script id="file-js" src="functions.js?v=1.4"></script>

Code to find all parameters :

var url = document.getElementById('file-js').src;
url = url.replace(/^.*?\?/, '');
var params = url.split('&');
for(var i in params) {
    var pair = params[i].split('=');
    console.log(pair);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744820402a4595580.html

相关推荐

  • Get query data in javascript from src attribute - Stack Overflow

    Often, when I update a JavaScript or CSS file used by a webpage, I will tack on v=<version>. This

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信