1

I'm trying to include a .js file like this:

<script src="javascriptfile.js"></script> 

But, what I really want, is to pass some parameters to my .js-file like this:

<script src="javascriptfile.js?id=3482"></script> 

How should I read the id= part from inside the .js file?

4
  • you are alreday doing it in second one Commented Jan 28, 2013 at 8:02
  • document.location.search gets the querystring, but I don't really think it's a good idea to do this. Commented Jan 28, 2013 at 8:04
  • 3
    @adeneo - that gets the document's querystring, not the script's querystring. Commented Jan 28, 2013 at 8:06
  • @jfriend00 - You're absolutely right, the JS is still executed on the document level. Makes it even a worse idea then ? Commented Jan 28, 2013 at 8:17

5 Answers 5

3

This is only possible by accessing "own" element in the HTML DOM and parse the src attribute.

here's a nice article with detailed explanations and code samples: http://feather.elektrum.org/book/src.html

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

Comments

3

Here a new way to do this.

You can use JSON.parse().

Here a example :

The code in the html :

<script id="scriptID" src="demo.js"> { "param1":"Hello World!", "param2":"paramLorem" } </script> 

The code in demo.js :

jQuery(document).ready(function(){ var myAttributes = JSON.parse(jQuery('#scriptID').html()); alert(myAttributes['param1']); }); 

You will get a alert with "Hello World!".

Hope it can help somebody!

3 Comments

perfect, works like magic.
I get the error ; expected on my semicolon if I do that
@annedroiid : You probably made a sythax error because it's work like a charm!
2

It is not simple to find your own script tag from within the javascript that was loaded by it. It is possible to search through all the script tags in the document and find the one that has your filename in it and parse out the query string, but it's much easier to just set a variable before the script loads like this:

<script>var javascriptfile_id = 3482;</script> <script src="javascriptfile.js"></script> 

And, then just check for the javascriptfile_id variable in your script.

3 Comments

It seems, that your code is wrong. You're mixing pure JS code with HTML. Where should that be placed? In .html file or in .js? You should outline first line of your code with <script></script> as well.
@trejder - I just added the missing script tgs.
Thanks! I couldn't do this myself, because most code changes in answers are usually dropped at review queue. We shouldn't use edit/review for changing code or answer meaning, only for simple corrections -- at least that's what they've been telling me all the time...
1

You could parse the parameters using jQuery, but I find that somewhat convoluted. Here's an example: http://wowmotty.blogspot.com/2010/04/get-parameters-from-your-script-tag.html

A better way to do it would be to create an initialize method in the JS file and call that initialize method with your paramters on page load.

Comments

0

You cannot pass a variable to the loaded script through the querystring, if it isn't intended for the server, so that a server-side script can pre-process the JavaScript before it is sent to the client.

If you want to pass a variable to the code within your external script, you would either have to make id a global JavaScript variable. Or you would have to call a function within the external script-file, passing the value as a parameter to that function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.