1

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?

2
  • 1
    Does this answer your question? Passing parameters to JavaScript files Commented Aug 17, 2020 at 11:41
  • @Cid it can work but the user only copy-paste generated script similar to above, I need to read the user id from only external js. the generated script tag should be in the above format may be with some single line changes Commented Aug 17, 2020 at 11:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

:) Thanks, it worked to get the URL after that I just followed gomakethings.com/… this page to achieve my requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.