3

On an HTML page I have a reference to an external Javascript file like this:

<script src="http://MyServer.com/js/myscript.js?Happy=True"></script>

Inside the myscript.js when it runs, can I get the Happy=True QueryString-like part of the js source URL?

Note I do not want the URL of the HTML page, I need to get the URL of the js file.

My Guess is no.

1 Answer 1

5

You will be able to detect the src-attribute of the script-element(would be easier to locate if the <script> has an ID). Out of that URL you could extract the Query-String.

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

4 Comments

I thought the same thing, but it seems (at least in Chrome) that the querystring gets stripped away. Not even getAttribute would get it. Just tested Firefox too. Same thing. jsfiddle.net/8G2Dm/1
Mmmh, I get the full path including the Query-String in Chrome(but it's V5, I think I'll need to update sometimes ^^)
...scratch that. Perhaps a jsFiddle issue? When I test locally, it works. +1
You need to access getElementsByTagName('script')[2] on jsfiddle, there are more scripts on jsfiddle embedded, the reported name is dummy.js, not myscript.js like expected.

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.