How to prevent onload function to fire more than once?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saps
    New Member
    • Nov 2010
    • 3

    How to prevent onload function to fire more than once?

    Hi All

    I have a onload event which calls a function in Javacsript which has some logic to be executing.

    Code:
    <body onload="display()">
    function display()
    {
    my logic goes here which creates a html table based on query results 
    }
    I have a text box with search button, when clicked calls another function. This works fine if i click the button direclty. But when i press enter key, it calls the required function and then automatically calls the onload function. This is happening only in firefox, works in IE.

    Code:
    display += '<input type="text" id="quick_Code" size="15" name="Template" onkeydown=\"javascript:if((event.which &amp;&amp; event.which == 13) || (window.event.keyCode &amp;&amp; window.event.keyCode == 13)){document.getElementById(\'btnSearch\').click();return false;}else return true;\" />';
    I'm not sure whats wrong with my code.
    Last edited by Niheel; Nov 30 '10, 05:27 PM. Reason: Please use code tags to display code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what kind of variable is display?

    Comment

    • saps
      New Member
      • Nov 2010
      • 3

      #3
      Display is a variable used to assign the HTML code to Div tag
      Code:
      <body class="Custom46Tab overviewPage" Onload="display();">
      <div class="" id="displayScreen"></div>
      </body>
      Code:
      document.getElementById("displayScreen").innerHTML = display;

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Doh, should have seen earlier.

        when you use display you’re actually referring to the display() function. you should definitely rename that. Do you have any messages in the error console?

        Comment

        • saps
          New Member
          • Nov 2010
          • 3

          #5
          Thanks Dormilich for your reply.
          Even renaming did not work for me.
          Problem is once the return key event triggers the button click, it calls the function and then automatically reloads the page. This works if I press the button manually by clicking it. Problem is only with firefox.

          Code:
          display += '<input id="btnSearch" class="btn" type="button" size="10" value="'Template Search" onclick="keywordSearch();"/>';

          Comment

          Working...