Passing parameters to JavaScript files -


I will often have a javascript file that I would like to use, for which to determine the definition of some variables in my web page needed.

So the code is something like this:

  & lt; Script type = "text / javascript" src = "file.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Var obj1 = "Some criteria"; & Lt; / Script & gt;  

But what I have to do:

  & lt; Script type = "text / javascript" src = "file.js? Obj1 = somevalue & obj2 = someothervalue" & gt; & Lt; / Script & gt;  

I have tried different ways and at best, we have to parse such query string as such:

  var scriptSrc = document.getElementById ("Myscript") .src.toLowerCase ();  

and then search for my values.

I am surprised that there is no other way to do this to create a function to parse my string.

> Do you know all of the other ways?

Use a namespace and OOP to pass your arguments through an object.

This code is in file.js:

  var MYLIBRARY = MYLIBRARY || (Function () {var_args = {}; // Personal Return {init: Function (ARG) {_args = Args; // Some other Initializing}, Hello World: Function () {Warning ('Hello World! -' _args [0]);}};} ());  

and in your HTML file:

  & lt; Script type = "text / javascript" src = "file.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; MYLIBRARY.init (["somevalue", 1, "control id"]); MYLIBRARY.helloWorld (); & Lt; / Script & gt;  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -