...

View Full Version : JS object expected error in function



kieran456
02-05-2007, 10:33 AM
Hi,

I am not very familar with Javascript and am trying to get the below working. It gives object expected errors.

Could someone take a look at the syntax of the function and show me whats wrong with it.

Thanks for all help...

-----------------------------------

<script>
function BeginPageLoad()
{
iLoopCounter = 0;
location.href = "<%= Request.QueryString("Page")%>";
iIntervalId = window.setInterval("iLoopCounter=UpdateProgress(iLoopCounter, iMaxLoop)", 500);
}
</script>

david_kw
02-05-2007, 04:39 PM
I believe that setInterval() wants a function for the first parameter and not a string.



<script>
function BeginPageLoad()
{
iLoopCounter = 0;
location.href = "<%= Request.QueryString("Page")%>";
iIntervalId = window.setInterval(function () { iLoopCounter=UpdateProgress(iLoopCounter, iMaxLoop); }, 500);
}
</script>


See if that still gives the error on the same line.

david_kw

TripperTreats
02-06-2007, 04:39 AM
I'm pretty sure you still need to put quotes around the function inside the setInterval. David may be right in that you need to call it all one function like he suggests, but I think the problem might be this:

When page loads, the browser gets an instruction to do a certain function every half-second. After it reads this instruction, the BeginPageLoad function is finished. Now, a half second later, it wants to try out this new function that it's supposed to do at intervals. However, the function asks it to store a value to iLoopCounter. It does not know what this variable is, because you defined it inside the BeginPageLoad function. It is a local variable. I believe you can solve your problem simply by placing iLoopCounter=0 outside any function, so that it parses as the script is loading, and not inside a one-time function.

liorean
02-06-2007, 05:13 AM
TripperTreats: The form with a string argument is strongly discouraged. It should be a function. Really.

kieran456: I bet your code gets executed in the wrong scope. iLoopCounter in the setTimeout function will not refer to the iLoopCounter of the BeginPageLoad function, for instance. You haven't given us enough code to see whether UpdateProgress and iMaxLoop also are declared in different scopes than the one setTimeout executes in, but it's possible.

Try david_kw's version and tell us if that works for you.

TripperTreats
02-06-2007, 01:47 PM
Liorean,
I was just curious as to why the quotes inside the setInterval is discouraged. I rarely need to use setIntervals, so I don't know much about it. As I said, I was under the impression that they were required. Does it have to do with the this keyword? Without knowing, I am wondering if it has anything to do with the difference between writing



document.onmousemove = function () {stuff1(x); stuff2(y)};
and


document.onmousemove = stuff1(x); stuff2(y);
Of course, I don't fully understand the differences between these, either. Thanks for any enlightenment.

david_kw
02-06-2007, 04:55 PM
document.onmousemove = stuff1(x); stuff2(y);

Of course, I don't fully understand the differences between these, either. Thanks for any enlightenment.

First of all, I don't believe that would work for several reasons. One is because it would be the same as



document.onmousemove = stuff1(x);
stuff2(y);


so stuff2(y) has nothing to do with the onmousemove assignment.

Also onmousemove expects a function to be assigned to it, not a string. I guess the setInterval function does the conversion for you if you pass in a string (I'm not sure why it is discouraged either).

One way you could set the onmousemove to a string is to do the conversion yourself like



document.onmousemove = new Function("stuff1(x); stuff2(y);");


I don't know if that helps explain stuff, but it is more information. :)

david_kw

liorean
02-06-2007, 07:05 PM
Liorean,
I was just curious as to why the quotes inside the setInterval is discouraged. I rarely need to use setIntervals, so I don't know much about it.First of all it's bad because, like eval, it has to fire up the JavaScript parser again and parse the string as a function body instead of using a function object directly. Second, it's bad because it evaluates as a function in the global scope and not in the local scope. Third, it's bad because it makes reasoning about the program more difficult.
As I said, I was under the impression that they were required. Does it have to do with the this keyword? Yes and no. setTimeout and setInterval make a regular function call and not a method call, and there's no way to get them to make a method call with either function or string argument. However, if you use a function as argument you can bind local variables and do method calls from there, which you cannot using the string form.
Without knowing, I am wondering if it has anything to do with the difference between writing



document.onmousemove = function () {stuff1(x); stuff2(y)};
and


document.onmousemove = stuff1(x); stuff2(y);
Of course, I don't fully understand the differences between these, either. Thanks for any enlightenment.The first is correct. The second doesn't do what you think it does. It first assigns the return value of stuff1(x) to the document.onmousemove event, and then it does stuff (y).

And assigning strings to even handler properties doesn't work. It may work in some browsers, but it doesn't work in general.

kieran456
02-13-2007, 11:19 AM
Hi,

Thanks for all the replies...

Sorry I couldnt reply sooner but was at a training course for last week and they refused us internet access due to security.

Anyway, here is the complete JS that is on the page...it is in the body tags. It is giving an object expected error. I moved the iLoopCounter out of the function and then the iMaxLoop. i tried the suggestion by david_kw but no joy.

The JS works by going to the loading page which has a 'please wait' message and then takes them to the news page when its loaded. /loading.aspx?Page=News.aspx

All help greatly appreciated...



<script>
iLoopCounter = 0;
iMaxLoop = 0;
function BeginPageLoad()
{
location.href = "<%= Request.QueryString("Page")%>";
iIntervalId = window.setInterval(function () { iLoopCounter=UpdateProgress(iLoopCounter, iMaxLoop); }, 500);
}

function EndPageLoad()
{
window.clearInterval(iIntervalId);
Progress.innerText = "Page Loaded -- Not Transferring";
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum