Passing Parameters to JavaScript Files

Passing parameters to JavaScript files

I'd recommend not using global variables if possible. Use a namespace and OOP to pass your arguments through to an object.

This code belongs in file.js:

var MYLIBRARY = MYLIBRARY || (function(){
var _args = {}; // private

return {
init : function(Args) {
_args = Args;
// some other initialising
},
helloWorld : function() {
alert('Hello World! -' + _args[0]);
}
};
}());

And in your html file:

<script type="text/javascript" src="file.js"></script>
<script type="text/javascript">
MYLIBRARY.init(["somevalue", 1, "controlId"]);
MYLIBRARY.helloWorld();
</script>

how to pass value to a js file

Not really, no. What you can do is this:

<script type="text/javascript">
var method = "create";
</script>
<script type="text/javascript" src="./js/create.js"></script>

Another way is to only define functions inside your javascript file, and then invoke after it has loaded.

<script type="text/javascript" src="./js/create.js"></script>
<script type="text/javascript">
runMyLoadedCode("create");
</script>

Third way is belying my first simplistic answer: access the script tag itself and parse it. You can see here how to access the tag that has loaded your script; take its src value and cut it up to locate your method=create.

Can I pass a parameter directly to a .js file, and how do I get the value

That's as far only possible by accessing "own" <script> element in the HTML DOM and parse the src attribute.

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

passing parameters to an external js files using javascript

The links you're providing are both imaginary. There is nothing present there, however the code that you're having is as:

(function () {

var styleEl = document.createElement("link");
styleEl.type = "text/css";
styleEl.href = "http://127.0.0.1:8002/static/css/widgets.css";
styleEl.rel = "stylesheet";

//document.getElementsByTagName('head')[0].appendChild(styleEl);
document.head.appendChild(styleEl);

document.write("<div id='share_box'>" +
"<a href='test' target='_blank'>R</a>" +
"</div>");
})();

Unable to pass a variable (In this example)

Actually, you cannot pass a parameter to it. Because it doesn't allow any.

How a function recieves a parameter

To recieve a parameter, the function must allow a parameter while declaration. Such as:

function someFuncName (params) {
/* then you can say that the parameters would be sent! */
}

The code you're saying, won't allow any parameters because there isn't any space or permission for a param.

How to pass a parameter

However, to pass on a param, you use the specific term

<script>
functionName(params);
</script>

Simple it is. But in your case, you cannot!

What the above code does:

What the above code actually does, is that is adds a link element. Such as

<link type="text/css" href="http://127.0.0.1:8002/static/css/widgets.css" 
rel="stylesheet" />

And the next thing it does, is that it would write an element clause of

<div id="share_box">
<a href="test" target="_blank">R</a>
</div>

Maybe the above code, would create a hyperlink and then style it using the .css file provided at the site that is inside the href.

But the answer is still, that you cannot pass the parameter to an external file until or unless it allows you to.

How to pass variable from one Javascript to another Javascript file

see about local and global variables for more info. http://www.w3schools.com/js/js_scope.asp.

Make sure your var X is not inside a function and that your file is load in the correct order.

<script src="file1.js"><script> //declare var x=1 here
<script src="file2.js"><script> // you can access x from here.

How to pass parameters to a Script tag?

Got it. Kind of a hack, but it works pretty nice:

var params = document.body.getElementsByTagName('script');
query = params[0].classList;
var param_a = query[0];
var param_b = query[1];
var param_c = query[2];

I pass the params in the script tag as classes:

<script src="http://path.to/widget.js" class="2 5 4"></script>

This article helped a lot.

Passing parameters from html script to js file

Variables defined in your html between scripts-tags will be registered on the window-scope. Therefore you can access those variables with window.idArray instead of AnActualVarFromHtmlScript.idArray.



Related Topics



Leave a reply



Submit