tags:

views:

25

answers:

2

below is function i want to use (function () { var url = param_url; })(); // what these ending curly brackets do ?

+1  A: 

The ending parentheses (()) call the function. You can pass arguments to it by putting them within the parentheses.

What you have there is a function expression which is then immediately called. The function expression is:

(function () { var url = param_url; })

...and then the parens call it. it's the same as:

var v = function () { var url = param_url; };
v();

...aside from the use of v, of course. So to pass an argument to it, just do this:

(function (argname) { var url = param_url; })(your_argument_here);

kangax has written up a useful article on function expressions, including browser bugs related to naming the function in the expression (amongst other things), which you should be able to but sadly, can't currently.

T.J. Crowder
A: 

T.J thank you very much for the quick reply actually i'm calling this script by including js file like script src="http://www.website.com/script.js?param_url=url"

and in file i have above function

Yasir
:) thanks i will follow your instructions next time but i could post code in comment that's why i create answer.
Yasir
If you read my first reply, apologies, I must have misread the question. (BTW, to ask follow-up questions or comment on answers, use the "add comment" below the answer rather than posting another answer.) No, that won't work, because query string parameters are not JavaScript function arguments. The JavaScript in question would need to access the query string (e.g., `window.location.search`).
T.J. Crowder
(You can post short snippets of code in backtick chars, `like this`.)
T.J. Crowder
can you please edit above code how i can access paramater through query string parameter when calling js file
Yasir
Man did I not have enough coffee this morning. :-) You can't get the query params *for the script* from `window.location.search`, that's for the document it's in. To get the query params on the script itself, you'd have to find the `script` tag in the document and read the `src` attr and parse it yourself. This isn't hard, but it's more than trivial. Fortunately, I just happen to know of an example: scriptaculous.js does that to load its submodules, more here: http://script.aculo.us and here: http://github.com/madrobby/scriptaculous/blob/master/src/scriptaculous.js (in the load function).
T.J. Crowder
:) i just created the variable above the script file call it worked great var url = 'http://www.website.com'; thank you so much T.J
Yasir
No worries. Happy coding.
T.J. Crowder
Thank you so much T.J because of you i understand this function.
Yasir