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?

trejder
  • 17,148
  • 27
  • 124
  • 216
Mads K
  • 847
  • 2
  • 14
  • 32

5 Answers5

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

Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
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!

Beauceron
  • 624
  • 5
  • 14
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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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 `` as well. – trejder Jan 12 '15 at 07:34
  • 1
    @trejder - I just added the missing script tgs. – jfriend00 Jan 12 '15 at 07:37
  • 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... – trejder Jan 12 '15 at 07:39
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.

Nick
  • 4,002
  • 4
  • 30
  • 41
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.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103