2

How can I read get parameters passed to a JS file like so:

<script type="text/javascript" src="/script.js?p1=hello&p2=world"></script>

In the same file (script.js) I want to read the p1 and p2 values like:

var p1 = params["p1"];

I'd rather avoid a method that getElementById's or uses the dom anyway. thanks.

4
  • Would it be possible for you to make a PHP wrapper that echoes the get parameters into the .js file? Commented Aug 8, 2011 at 15:53
  • This might be interesting: stackoverflow.com/questions/4716612/… Commented Aug 8, 2011 at 15:54
  • @pimvdb no, the purpose is making it front-end only. Commented Aug 8, 2011 at 15:55
  • Take a look at this [enter link description here][1] [1]: stackoverflow.com/questions/2328864/… Commented Aug 8, 2011 at 16:00

2 Answers 2

3

No magic that I'm aware of here... As the script is loaded, the tag is added to the dom tree and you can read from it using document.getElementsByTagName('script') and then parse the results.

Full code given here : http://feather.elektrum.org/book/src.html

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

1 Comment

Thanks, I wanted to make sure there wasn't anything similar to window.location that pertains to the .js file itself. Used getElementById eventually.
-1

See this answer. You then have to parse the result yourself.

How to check for script src match, then reassign src

1 Comment

This will not work, cause script[i].src contains full URL with all params, so you don't have loop through all scripts' srcs if you already know query string

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.