Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Question Need alternate to the "documnt.write" line.

    Hey guys, I need help... I want to have this script

    Code:
    <script type = "text/javascript">
    function runMyJavascript_Caught() 
    {
    // get user to add name
    var name_entry = prompt ("Please Enter your Name!", "James");
    
    // declare some short strings
    
    var stating = ", I knew you did it!";
    var told = "I told you that I would find out";
    var mistake = "I guess you just made a mistake entering your name, ";
    var punish = "Now go back to your room";
    var heading = "OI Stop";
    
    // construct some longer strings
    
    var stating_name = name_entry + stating;
    var mistake_name = mistake + name_entry;
    
    document.writeln(heading.bold() + "<br>");
    document.writeln(stating_name + "<br>");
    document.writeln(told + "<br>");
    document.writeln(mistake_name + "<br>");
    document.writeln(punish + "<br>");
    }
    </script>
    write on a button command. What can I use insted of document.write?
    Last edited by MancunianMacca; 03-02-2012 at 06:15 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,574
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Time (past time!) to learn about the DOM.

    Code:
    var element = document.getElementById("idOfSomeElementOnThePage");
    element.innerHTML ="<b>" + heading + "</b><br>" + stating_name + "<br>" 
                     + told + "<br>" + mistake_name + "<br>" + punish + "<br>";
    But there are other and, some say, better ways.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by MancunianMacca View Post
    What can I use insted of document.write?
    createElement() and createTextNode() are DOM methods you could use instead of document.write().

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    innerHTML works fine as long as you don't subsequently need to reference any of the added elements from JavaScript and as long as you don't try to add part of a table. Either of those will not work in all browsers.

    It will work for anything that used to use document.write and was the quickest way of disposing of all the document.write statements after Netscape 4 dies in 2005. It made the complete disappearance of the document.write statement by early 2006 possible (ore would have if people actually bothered to update prehistoric scripts rather than continuing to create more).

    Anyone learning JavaScript in recent years should be spending about 1/3 of the course learning all the different DOM commands as that is now one of the largest and certainly the most useful part of JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Thumbs up

    Quote Originally Posted by felgall View Post
    innerHTML works fine as long as you don't subsequently need to reference any of the added elements from JavaScript and as long as you don't try to add part of a table. Either of those will not work in all browsers.

    It will work for anything that used to use document.write and was the quickest way of disposing of all the document.write statements after Netscape 4 dies in 2005. It made the complete disappearance of the document.write statement by early 2006 possible (ore would have if people actually bothered to update prehistoric scripts rather than continuing to create more).

    Anyone learning JavaScript in recent years should be spending about 1/3 of the course learning all the different DOM commands as that is now one of the largest and certainly the most useful part of JavaScript.
    Thanks for the help guys I resolved this


  •  

    Posting Permissions

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