Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back a page or two to re-sync the thread and this will then show latest posts. Thanks, Mike.

Cant get body onload to fire

  • 27-02-2009 11:31am
    #1
    Registered Users, Registered Users 2 Posts: 2,791 ✭✭✭


    Hi,

    I have some simple JS that I cant get to fire on my ASP.net Masterpage.

    It's the same for FF3 and IE7, havent checked in other browsers yet.

    There are no JS errors, just nothing is happening...
    <script type="text/javascript">
        function showTerms() {
          alert("got here");
          var termsContainerID = '';
    
          if ($get('hdnShowTerms') != null)
            termsContainerID = $get('hdnShowTerms').value;
            
          if ($get(termsContainerID) != null) {
            $.modal(document.getElementById(termsContainerID), { overlayId: ('TermsModal-overlay'), containerId: ('TermsModal-container') });
          }
          
        }
      </script>
    </head>
    
      <body onload="showTerms();" >
    

    Anyone see anything wrong with this? :confused:


Comments

  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    Works fine as is in my FF3 and IE7 although I did have to allow ActiveX content on IE7 before it worked. Could your security settings be stopping it?
    <html>
    <head>
    <script type="text/javascript">
        function showTerms() {
          alert("got here");
          var termsContainerID = '';
    
          if ($get('hdnShowTerms') != null)
            termsContainerID = $get('hdnShowTerms').value;
            
          if ($get(termsContainerID) != null) {
            $.modal(document.getElementById(termsContainerID), { overlayId: ('TermsModal-overlay'), containerId: ('TermsModal-container') });
          }
          
        }
      </script>
    </head>
    
      <body onload="showTerms();" >
    hello
    </body>
    </html>
    

    (ETA: I get the "got here" alert, obviously nothing else works)


  • Registered Users, Registered Users 2 Posts: 21,257 ✭✭✭✭Eoin


    Works on FF and IE for me as well.


  • Registered Users, Registered Users 2 Posts: 2,791 ✭✭✭John_Mc


    Thanks lads, been trying to the past two days to get a JS function onto a page should a server side function decide that on needs to be added. I used the ClientScript Startup method which worked in FF, but caused an "operation aborted" error in IE :(

    I've had to go about things in a completely different way now and it's working. Still dont know why the onload method wasnt called though...


  • Closed Accounts Posts: 2,300 ✭✭✭nice1franko


    AFAIR, the body onload event is a bit flaky (may not fire if any dom elements in it are edited before the event would naturally fire or something)... try document.onload instead.


  • Moderators, Science, Health & Environment Moderators Posts: 9,008 Mod ✭✭✭✭mewso


    Since we are now in 2009 I thought I would make a few suggestions.

    1. Stop using inline javascript in html.
    2. Put your script in external js files.
    3. Write your script to be unobtrusive.
    4. Use a proper well documented method to add load events to your page like the addLoadEvent function or putting your script link at the end of the body content.
    5. Please stop using inline javascript in html.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,119 ✭✭✭p


    I'd echo wat musician has said.

    Put the onload event in the javascript, not the HTML and you'll be writing much better practice JS.

    http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html


Advertisement