Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS object expected error in function

    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>

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe that setInterval() wants a function for the first parameter and not a string.

    Code:
    <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

  • #3
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

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

    Code:
    document.onmousemove = stuff1(x); stuff2(y);
    Of course, I don't fully understand the differences between these, either. Thanks for any enlightenment.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TripperTreats View Post
    Code:
    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

    Code:
    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

    Code:
    document.onmousemove = new Function("stuff1(x); stuff2(y);");
    I don't know if that helps explain stuff, but it is more information. :)

    david_kw

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by TripperTreats View Post
    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

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

    Code:
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New Coder
    Join Date
    Jul 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •