I have
<script type="text/javascript" src="./js/rendering.js?id=3">
I want to get id value in my rendering.js how can I achieve this?
I have
<script type="text/javascript" src="./js/rendering.js?id=3">
I want to get id value in my rendering.js how can I achieve this?
To get the URL of the current JavaScript file you can use the fact that it will be the last <script>
element currently on the page.
var scripts = document.getElementsByTagName('script');
var script = scripts[scripts.length - 1];
var scriptURL = script.src;
Please note that this code will only work if it executes directly within the JS file, i.e. not inside a document-ready callback or anything else that's called asynchronously. Then you can use any "get querystring parameter" JS (but make sure to replace any location.search
references in there) to extract the argument.
I'd suggest you to put the value in a data-id
argument though - that way you can simply use e.g. script.getAttribute('data-id')
to get the value.