0

I'm trying to pass two parameters to a JavaScript function. Unfortunately this give me error.

Here is the function

 function load(do,div)
 {
 var do;
 var div;
 document.getElementById('spinner').innerHTML = '<img src=\'/images/spinner.gif\'>';
 $('#'+div).load('rpc.php?'+do,
 function()
 {
 document.getElementById('spinner').innerHTML = '';
 $('#'+div).show('slow');
 }
 }

And I call it like this:

 <a href="javascript:;" onclick="load('limit','mydiv');">Get Limit</a>

How can I fix that?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Dec 25, 2009 at 20:24

3 Answers 3

11

do is a reserved word in JavaScript. Change the variable name to something else. Additionally, don't re-declare the arguments in the function body. So remove the 2 var lines from the top of your function body.

If you're curious what the do keyword is for, it's for do...while loops where the condition is evaluated at the end, not the beginning of the loop. It's used something like this:

do {
 // do stuff in loop at least once
} while (some_condition_is_true);

For more info check out W3Schools.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Dec 25, 2009 at 20:26
Sign up to request clarification or add additional context in comments.

5 Comments

... and remove the two declarations which are the overriding the arguments.
re-declaring variables with same argument name, doesn't effect the arguments.
@Luca Matteis: Interesting. I didn't know that. Even so, it's unconventional enough that I still advise removing those lines.
Interesting. This is a-logical.
@BalusC: What exactly is a-logical? The fact that re-declaring variables has no effect, or my decision to leave the recommendation to remove the re-declaration in my answer despite knowing the former?
2

do is a reserved word in JavaScript.

http://javascript.about.com/library/blreserved.htm

answered Dec 25, 2009 at 20:28

Comments

1

Is it because you are redefining do div again in function and they are overriding the scope of passed parameters?

answered Dec 25, 2009 at 20:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.