Get the query string on the called javascript file

You may append id attribute to script tag like this:

<script src="https://stackoverflow.com/file.js?query=string" id="query"></script>

and then call it:

console.log(document.getElementById("query").src.split("query=")[1]);

A small working sample code is below:

<html>
<head>
<script src="https://stackoverflow.com/questions/15223884/aaa.js?query=abcd" id="query"></script>
</head>
<body></body>
</html>

Here is the code inside of aaa.js:

window.onload=function(){
    alert(document.getElementById("query").src.split("query=")[1]);
}

Leave a Comment