Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 06:10 PM   PM User | #1
jasonmcbee
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
jasonmcbee is an unknown quantity at this point
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..
jasonmcbee is offline   Reply With Quote
Old 02-12-2013, 10:02 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
jasonmcbee (02-13-2013)
Old 02-12-2013, 10:15 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-12-2013, 10:19 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
jasonmcbee (02-13-2013)
Old 02-13-2013, 01:26 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-13-2013, 03:42 PM   PM User | #6
jasonmcbee
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
jasonmcbee is an unknown quantity at this point
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..
jasonmcbee is offline   Reply With Quote
Old 02-13-2013, 03:52 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 02-13-2013 at 03:55 PM..
Philip M is online now   Reply With Quote
Old 02-13-2013, 04:05 PM   PM User | #8
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,697
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:15 AM.


Advertisement
Log in to turn off these ads.