0

I wanna know is this possible I pass get parameters for a javascript file and use them in codes? I have this in html:

<script type="text/javascript" src="/javafile.js?q=somtext"></script>

know how I use this "q" parameter in my script codes? is this possible?

1
  • yes it seems it is dublicated :P Commented Feb 15, 2012 at 14:15

3 Answers 3

4

You can either:

  1. Generate the JS dynamically with a server side language and read the query string there
  2. Assume that the JS is the last <script> element so far in the DOM and parse document.scripts[document.scripts.length - 1].src

Note that if the script element has been added dynamically (i.e. with JS), it might not be the last script in the DOM.

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

2 Comments

thanks a lot. I seen in firebug that every javascript file in request send with a "_" parameter that have a a time-stamp. what do this parameter?
@IVIR3zaM — That isn't standard. Something is probably adding it as a cache buster.
1

I think Quentin's suggestion is the answer to your question.

I usually use an alternative way for this, which may also help you:

Make sure that your javascript is written in a library form, and make sure that you have an instantiate method/function inside your javascript that allows you to pass parameters (or better, as an object)

// on dom load:
library.init({ var1: value1, var2: value2});

This allows you also to load your javascript in a different way, and allows for cleaner code.

Comments

0

Or you can use option 3: use a library that has this functionality, e.g. MooTools: http://mootools.net/docs/more/Types/String.QueryString

1 Comment

It looks like it's just an utility function for string -> object conversion; you'd still have to obtain the actual path with the values first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.