Page view counter

Call javascript after page load??

Last post 11-14-2006 8:59 AM by RoamingLlama. 17 replies.

Sort Posts:

  • Call javascript after page load??

    11-08-2006, 9:05 AM
    • Loading...
    • ike2010
    • Joined on 05-17-2005, 11:58 AM
    • Posts 79
    • Points 360

    Hello all.  I'm trying to fire a javascript function after all of the page controls have been loaded to get a count of all the <span> elements on the page.  Javascript function:

    function spanCount()
    {
        var spans = document.getElementsByTagName('span');
        alert('# of spans: " + spans.length);
    }

    I have the following after the </form> element in the .aspx page:

    <script language="javascript">
        window.onload = spanCount();
    </script>

    The error I receive is: Object expected

    Any ideas on how I can get this to work?
     

  • Re: Call javascript after page load??

    11-08-2006, 11:52 AM
    Answer
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,681
    • Points 18,331
    • TrustedFriends-MVPs

        Can you get a line number?  Where is the spanCount function located?

        Also, I'm not sure that assigning the onload event with parentheses in the function call is the right way to go; that might try to execute the function right away.  What happens if you put the assignment (sans parens) into the onload event handler of the body tag instead?

    HTH, 

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Call javascript after page load??

    11-08-2006, 1:41 PM
    • Loading...
    • RoamingLlama
    • Joined on 11-06-2006, 1:05 PM
    • Akron, OH
    • Posts 40
    • Points 210

    ike,

    The error is caused because you have used a single quote instead of a double quote...line should be:

    alert("# of spans: " + spans.length);

    not

    alert('# of spans: " + spans.length);

    Thanks,
    Dan
     

    Thanks,
    Dan
  • Re: Call javascript after page load??

    11-09-2006, 6:06 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 10:01 AM
    • The World's End
    • Posts 1,725
    • Points 8,569

    Hello ,

    in JavaScript, single and double quotes are equivalent in delimiting strings, provided they match of course.

    As Peter suggested, the problem is that ike2010 is invoking the method here, rather than passing a reference:

       window.onload = spanCount();

    It should be:

       window.onload = spanCount;

    Hope this helps. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Call javascript after page load??

    11-09-2006, 9:26 AM
    • Loading...
    • RoamingLlama
    • Joined on 11-06-2006, 1:05 PM
    • Akron, OH
    • Posts 40
    • Points 210

    While passing the reference is the preferred method of doing this, it will still work with the parenthesis as he has done (at least in my testing of his code).

    -Dan 

    Thanks,
    Dan
  • Re: Call javascript after page load??

    11-09-2006, 9:52 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 10:01 AM
    • The World's End
    • Posts 1,725
    • Points 8,569

    RoamingLlama:
    While passing the reference is the preferred method of doing this, it will still work with the parenthesis as he has done (at least in my testing of his code).

    It is not simply "preferred", it is the "correct" way to do it. The code "works" is never a valid principle. If your test worked is just because you put the JavaScript at the bottom of the page, where the spanCount is ready to run, and it is not running upon onload, it is simply running in-line!

    In general, I would point out that: 1) here we are supposed to give programming advice, not just "make stuff work in my test"; 2) client-side programming is not at all easy for quite a lot of reasons, and it is real programming under all respects. Would you be so imprecise regarding C# in instance?

    -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Call javascript after page load??

    11-09-2006, 1:17 PM
    • Loading...
    • RoamingLlama
    • Joined on 11-06-2006, 1:05 PM
    • Akron, OH
    • Posts 40
    • Points 210

    LV,

    I understand your point and in future posts I will more thoroughly review my response to make sure that it is proper coding and not just hacking it together.  While it is not my strongest language (C# and PHP are my strongest) I do know JavaScript fairly well and should have thought more clearly about what I suggested.

    Thanks,
    Dan
     

    Thanks,
    Dan
  • Re: Call javascript after page load??

    11-09-2006, 6:45 PM
    • Loading...
    • TrafficSchool
    • Joined on 11-09-2006, 11:35 PM
    • Posts 10
    • Points 50
    <body onLoad="alert('This page has finished loading!)">
     Hope this works
    
  • Re: Call javascript after page load??

    11-09-2006, 11:09 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,681
    • Points 18,331
    • TrustedFriends-MVPs

    How does that address his question?

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Call javascript after page load??

    11-09-2006, 11:33 PM
    • Loading...
    • lordtjhai
    • Joined on 11-10-2006, 4:05 AM
    • Indonesia
    • Posts 8
    • Points 40

    As I see, the only problem here is that you used a single quote to start your alert,
    but then you used double quote to end your alert.
    You should have used just one pair of them. Whether single qoute pair or double qoute pair, it doesn't matter.
    That's all you have to do. Nothing else.

     Please take a look at my code:

    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <script language="javascript">
    function spanCount()
    {
        var spans = document.getElementsByTagName('span');
        alert('# of spans: ' + spans.length);
        //you can also put
        //alert("# of spans: " + spans.length);
        //here
    }
    </script>

    <head runat="server">
        <title>Test</title>
    </head>
    <body>
    <script language="javascript">
        window.onload = spanCount();
    </script>
        <form id="form1" runat="server">
                <span id='span1'>test</span>
        </form>
    </body>
    </html>
    simarmas group - forestry division
    Application Developer
    ________________________________________
    No violence in our food
    No violence in our world
    Viva Vege!!!
  • Re: Call javascript after page load??

    11-10-2006, 12:10 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,681
    • Points 18,331
    • TrustedFriends-MVPs

    No.  That is wrong.  Please read what LudovicoVan wrote above.

        The "object expected" was being thrown because the way the event assignment is written, he is actually calling the spanCount function and assigning its return value to the onload attribute.  Since event handlers don't work that way, and-- more importantly -- there is no value returned from the function, you get an "object expected" error.

        The quotes do need to be matched correctly, but that is not what was causing the error mentioned in the original post.

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Call javascript after page load??

    11-10-2006, 2:06 AM
    • Loading...
    • lordtjhai
    • Joined on 11-10-2006, 4:05 AM
    • Indonesia
    • Posts 8
    • Points 40

    Oh, sorry. You were right.
    I don't really take a good look before.
    I just put my attention on that quote matter.

    But I think by joining TrafficSchool's sugesstion and RoamingLlama's,
    we can have the right coding, can't we?
    I mean by changing the quote as the same pair (whether single or double quote),
    and on body tag, we put onLoad to call spanCount() function.

    <body onload="spanCount();">
     

    simarmas group - forestry division
    Application Developer
    ________________________________________
    No violence in our food
    No violence in our world
    Viva Vege!!!
  • Re: Call javascript after page load??

    11-10-2006, 11:17 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 10:01 AM
    • The World's End
    • Posts 1,725
    • Points 8,569
    PeterBrunone:

    No.  That is wrong.  Please read what LudovicoVan wrote above.

        The "object expected" was being thrown because the way the event assignment is written, he is actually calling the spanCount function and assigning its return value to the onload attribute.  Since event handlers don't work that way, and-- more importantly -- there is no value returned from the function, you get an "object expected" error.

        The quotes do need to be matched correctly, but that is not what was causing the error mentioned in the original post.

    This thread tells a nice story, for those who care reading of course. There is also a bit of irony i guess. :)

    Peter, I didn't say that. I am quite good at skipping what is not my current concern and still "close the gaps", so to say. If I furtherly inspect the code, I would expect 2 errors actually: 1) The misquoting should make definition of the function fail along with all subsequent definitions in that SCRIPT, with a complain about unterminated string literal; 2) That should in turn lead to an error when the code invokes (and that is really error number 2, about misunderstanding references) a function that is not defined due to error 1. That might be giving "object expected", or rather "object undefined"...  But, I still have made no test here, and that I guess would be the only serous way to give a detailed answer in similar cases, due to all the browsers idiosyncrasies we sadly know of.

    Actually, my basic point was about being a bit more careful in giving answers!!! If we all look at it for more than few seconds, we would see that most people here do not even conceive the possibility of this being "programming", and "debugging" their scripts. Most do not even really know how to look at the errors the browser reports. Mostly simply cut-and-paste and let's see if I can hack it. So, another crusade maybe, yet I feel the need of it from times to times...

    I would again stress a point that is largely misunderstood:

    This:
       <body onload="doSomething()">

    is NOT equivalent to this:
       body.onload = doSomething(); // Incorrect!!!

    The correct way to pass a reference is this:
       body.onload = doSomething;

    Now you see, this is an instance of "closing the circle" while not getting into details. That is correct and can lead to any further correct argument, so that I am in a rock there, yet have not taken the time to say it all. Go study for that, or at least ask more...

    But, but, but, that said, I must admit at the beginning, err... I simply didn't see/didn't care the misquoting - my under-brain knows! I actually misred RoamingLlama first post at all, as I thought he was suggesting single quotes in place of double quotes. I apologize for that! And, btw, you get the irony...

    -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Call javascript after page load??

    11-10-2006, 11:41 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,681
    • Points 18,331
    • TrustedFriends-MVPs

        Well, sure, we both missed the quote part, and somebody else pointed it out early on.  That's why forums are open for everyone to answer Smile

        However, the other problem was still the reason for the error, and without fixing it, you would still have errors.  Therefore the statement that correcting the quotes is "all you have to do.  nothing else" was incorrect, and more noticeably so after reading the first few posts of the thread. 

        But like I said, that's why we have forums; sooner or later, through the efforts of several people (or five, in this case), we'll get you to the right answer.

     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Call javascript after page load??

    11-13-2006, 9:33 PM
    • Loading...
    • lordtjhai
    • Joined on 11-10-2006, 4:05 AM
    • Indonesia
    • Posts 8
    • Points 40

    Oh, c'mon Peter. I have tried to correct my mistaken already.
    So what do you suggest to me now :)
    For what I knew, the

    <body onload="doSomething()">

    means, while the body is onloading, call the doSomething method.
    Or have I made a mistake anymore?
    And.. What's the resolution then?
    Let's not make ike2010 confused ;)

    simarmas group - forestry division
    Application Developer
    ________________________________________
    No violence in our food
    No violence in our world
    Viva Vege!!!
Page 1 of 2 (18 items) 1 2 Next >