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 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Smile Help! What is wrong with this code?? (Variables and Strings)

    Code:
    <html>
    <head>
    <title> Caught </title>
    
    <script language="JavaScript">
    <-- hide me
    
    // 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"
    
    // construct some longer strings
    
    var stating_name = name_entry + stating;
    var mistake_name = mistake + name_entry;
    
    // stop hiding me -->
    </script></head><body>
    
    <h1> OI Stop! </h1>
    
    <script language="JavaScript">
    <!-- hide me
    
    document.writeln(stating_name + "<br>");
    document.writeln(told = "<br>");
    document.writeln(mistake_name = "<br>");
    document.writeln(punish = "<br>");
    
    // show me -->
    </script>
    </head>
    <p>
    <FORM>
    <INPUT TYPE="button" VALUE="To Your Room" onClick="history.back()">
    </FORM>
    
    </p>
    </body></html>
    It will not show my strings and variables?? Please Help!!
    Last edited by MancunianMacca; 02-28-2012 at 09:27 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You should use your error console to reveal numerous errors.

    Code:
    <html>
    <head>
    <title> Caught </title>
    
    
    <script type = "text/javascript">
    
    // 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";
    
    // construct some longer strings
    
    var stating_name = name_entry + stating;
    var mistake_name = mistake + name_entry;
    
    document.writeln(stating_name + "<br>");
    document.writeln(told + "<br>");
    document.writeln(mistake_name + "<br>");
    document.writeln(punish + "<br>");
    
    </script>
    
    </head><body>
    
    <h1> OI Stop! </h1>
    
    </body></html>
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript">. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


    Bookshop Assistant: "This book about Javascript will save you half your work".
    Lazy Student: "Oh good! I'll take two!"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    MancunianMacca (02-28-2012)


  •  

    Posting Permissions

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