SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.write()

    i am trying to call a method that does a document.write() from an onclick on a button. the document.write() is overwriting everyting on the page. i just want to add it to the stuff already on the page. when i call the function by itself (not from the onclick) it appends instead of rewriting. anyone familiar with this? thx

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't use document.write() after the current document is fully loaded (and therefore, closed). Using document.write() after pageload creates a new document and writes to it, which is why your page goes blank -- the new document replaces the current one.

    You will need to use the DOM to manipulate and/or create text and elements instead.

    What sort of stuff are you adding?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nails 11.

    Here is an example of how I insert text on the fly, should help you I think..

    Hey anyone know how to paste HTML code into a reply.. oh well.


    <html>
    <table>
    <td>
    Test of inserting Text
    <td>
    <td>
    <input type = button name=cmdInsert onclick="insertText()">
    </td>
    <td>
    <div name="insertedText" id=insertedText></div>
    </td>

    <script>
    function insertText()
    {
    insertedText.innerHTML = "Inserted Text"
    }
    </script>

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark_Elford
    Hey anyone know how to paste HTML code into a reply...
    Hi Mark, look at the code and html tags on this page.

  5. #5
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhhh, I see so if I do This it should paste in correctly instead of actually giving the output of my code. : -

    HTML Code:
    <html>
    <table>
    <td>
    Test of inserting Text
    <td>
    <td>
    <input type = button name=cmdInsert onclick="insertText()">
    </td>
    <td>
    <div name="insertedText" id=insertedText></div>
    </td>
    
    <script>
    function insertText()
    {
    insertedText.innerHTML = "Inserted Text"
    }
    </script>
    Thanks for that.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    insertedText.innerHTML = "Inserted Text"
    Only works in Internet Explorer! The W3C way (OK, sorta):
    Code:
    document.getElementById('insertedText').innerHTML = "Inserted Text"
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    p {font: 600 16px verdana; color: #6699cc;}
    
    </style>
    <script type="text/javascript" language="javascript">
    
    function insert_it(what, where)
    {
    	where = document.getElementById(where);
    	where.innerHTML = what;
    }
    
    var imgtag = '<img src="http://www.sitepointforums.com/images/icons/icon6.gif">stuff';
    
    </script>
    </head>
    <body>
    <p></p>
    <p>
    i just want to add it to the <span id="poke">
    <img src="http://www.sitepointforums.com/images/icons/icon5.gif"></span> already on the page.
    </p>
    <input type="button" value="stuff" onclick="insert_it(this.value,'poke')">
    <input type="button" value="junk" onclick="insert_it(this.value,'poke')">
    <input type="button" value="incredible mishmash" onclick="insert_it('<i>' + this.value + '</i>','poke')">
    <input type="button" value="HTML" onclick="insert_it('<small>' + this.value + '</small>','poke')">
    <input type="button" value="kewl" onclick="insert_it(imgtag,'poke')">
    </body>
    </html>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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