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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    I need help with script

    I cant get this to work the book is too cut and dry please help.

    Code:
    <!DOCTYPE html PUBLIC
    	"-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Two Functions</title>
    <meta http-equiv="content-type" content="text/html; 
    charset=iso-8859-1" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function printMessage(first_message) {
    document.write("<p>" + first_message + "</p>");
    }
    function return_message() {
    return "<p>This message was returned from
    a function.</p>");
    /* ]]> */
    </script>
    </head>
    <body>
    <script type="text/javascript">
    /* <![CDATA[ */
    printMessage("This message was printed
    by a function.");
    var return_value = return_message();
    document.write(return_value);
    /*]]> */
    </script>
    </body>
    </html>

    its very simple but i cant figure out where im going wrong
    Last edited by jasonmcbee; 02-13-2013 at 03:37 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Have you tried using your error console (F12)?

    Be aware that document.write() has been obsolete since Netscape 3 passed away 10+ years ago. 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.


    BTW, when posting here please help us to help you by making it easier to copy, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


    Quizmaster: Which Russian leader is credited as the architect of glasnost?
    Contestant: Stalin

    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:

    jasonmcbee (02-13-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    But Philip, as ill-advised as his code is, he is *NOT* invoking document.write after the page is load at any point in that code.

    He has various OTHER problems on that page. Such as a missing }. And more.
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Corrections made:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Two Functions</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function printMessage(first_message) 
    {
        document.write("<p>" + first_message + "</p>");
    }
    function return_message() 
    {
       return "<p>This message was returned from a function.</p>";
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <script type="text/javascript">
    /* <![CDATA[ */
    printMessage("This message was printed by a function.");
    var return_value = return_message();
    document.write(return_value);
    /*]]> */
    </script>
    </body>
    </html>
    Note that in your post you had these lines:
    Code:
    return "<p>This message was returned from
    a function.</p>");
    
    printMessage("This message was printed
    by a function.");
    I can't tell if that's an artifact of how you posted or if your code is really like that.

    If it's really like the, you need to know that a JavaScript string literal (text between quote marks) can *NOT* be broken across more than one line.

    If the text is long, you must break it into two literal strings and concatenate them.

    Example:
    Code:
    printMessage("This message was printed"
            + " by a function.");
    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.

  • Users who have thanked Old Pedant for this post:

    jasonmcbee (02-13-2013)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The code makes no sense at all.

    <![CDATA[ ]]> is only valid for pages served as XHTML - in those pages it should not be commented out. XHTML also does not allow document.write to be used at all and so having both of those in the same script is completely invalid.

    The only use for commenting out the CDATA tag is where you have a page currently being served as HTML that you intend serving as XHTML as soon as IE8 dies (the only browser that still has a significant following that doesn't support XHTML). The script code should therefore be written ready for that conversion which again means that document.write is completely invalid.

    Which book was it that presented such meaningless code?
    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.

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks for all the help. As you can tell I am new to JS. I know the document.write is outdated but again the assignment gave me all the code I just had to put it in the right order and debug it. The text book they have us follow is very hard to follow it jumps around to much. The book is also 2008 edition.



    Javascript fifth edition by gosselin
    Last edited by jasonmcbee; 02-13-2013 at 03:44 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by jasonmcbee View Post


    Javascript fifth edition by gosselin
    A book to avoid, methinks.

    jason - you seem to have deleted your first post which introduces this thread. That is very much frowned upon. It has effect of making the thread largely meaningless.
    Last edited by Philip M; 02-13-2013 at 03:55 PM.

    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.

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,777
    Thanks
    6
    Thanked 1,021 Times in 994 Posts
    It hasn’t been deleted, it has been “unapproved” automatically by the spam filter after the OP has modified it in some way, so it wasn’t visible to the general public anymore. I’ve (re-)approved it now. Thanks for letting us know, Philip.


  •  

    Posting Permissions

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