onclick and document.write interaction--code doesn't work a second time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • david l smith
    New Member
    • Aug 2010
    • 2

    onclick and document.write interaction--code doesn't work a second time

    Hi,

    The following code snippet works on page load (it displays an anchor link using document.write) , works when the anchor is clicked on the first time, but does not work when the anchor link is clicked on subsequently:

    I get the error "object expected" with the following in the left side of the debug screen when the link is clicked on a second time:
    Code:
    <A href="javascript:void(0)" onclick="MyFunc()">Press Here</A>
    Does anybody know why this code does not work properly when the anchor link is clicked on a second time?

    Any help would be greatly appreciated.. The server is IIS 5.1 under Win XP SP3. The client browser is IE8.

    Thanks,

    Dave
    ---------------------------------------------------
    Code:
    <HTML>
    <BODY>
    
    <SCRIPT>
    RedrawScreen()
    
    function RedrawScreen() {
    alert("in the draw screen function")
    document.write("<A href=javascript:void(0) onclick=MyFunc()>Press Here</A>")
    }
    
    function MyFunc() {
    alert("in the function")
    RedrawScreen()
    }
    
    </SCRIPT>
    </BODY>
    </HTML>
    Last edited by gits; Aug 1 '10, 08:09 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    when you use document.write( ) after a page's DOM was ready then you destroy that with writing again to it. you should use proper DOM-methods to add new nodes to the document like createElement(), appendChild() etc. for such tasks - in that case the page would remain intact.

    Comment

    • david l smith
      New Member
      • Aug 2010
      • 2

      #3
      Originally posted by gits
      when you use document.write( ) after a page's DOM was ready then you destroy that with writing again to it. you should use proper DOM-methods to add new nodes to the document like createElement(), appendChild() etc. for such tasks - in that case the page would remain intact.
      Thanks for the info, that clears up a lot.

      Thanks again,
      Dave

      Comment

      Working...