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 03-13-2013, 03:34 PM   PM User | #1
Tacitus86
New to the CF scene

 
Join Date: Mar 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Tacitus86 is an unknown quantity at this point
Beginner Javascript doc write question

Hi all. I'm an experienced C and Java programmer that is trying my hand at Javascript. I'm writing some very basic trial programs and was curious why my document.write elements disappear when I add any kind of other javascript code.

For example, this works and I see the output.

Code:
<HTML>
	<p id="intro">
		Hello World!
	</p>
	<script>

                document.write("<h1>This is a heading</h1>");
		document.write("<p>This is a paragraph</p>");	               
		
	</script>

	<button type="button" onclick=textChanger()> Don't click this button! </button>

</HTML>
But the output disappears when I add a simple var... why?

Code:
<HTML>
	<p id="intro">
		Hello World!
	</p>
	<script>

                var switch;

                document.write("<h1>This is a heading</h1>");
		document.write("<p>This is a paragraph</p>");	
               
		
	</script>

	<button type="button" onclick=textChanger()> Don't click this button! </button>

</HTML>
I read that you can't have a document write inside a function or occur after the page has been created without clearing all the html, so I'm aware of that caveat. But this just removes my doc write calls, the html text is still there.
Thanks for the help!
Tacitus86 is offline   Reply With Quote
Old 03-13-2013, 03:40 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
actually, the problem is that switch is a reserved keyword in javascript:

https://developer.mozilla.org/en-US/...tements/switch

so what is really happening is that your code is breaking before it gets to the document.write statements. Choose another variable name and your code works as expected:
Code:
<HTML>
	<p id="intro">
		Hello World!
	</p>
	<script>

                var swatch;

                document.write("<h1>This is a heading</h1>");
		document.write("<p>This is a paragraph</p>");	
               
		
	</script>

	<button type="button" onclick=textChanger()> Don't click this button! </button>

</HTML>
Your error console is your best friend at this point
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
Tacitus86 (03-13-2013)
Old 03-13-2013, 03:43 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,306
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
switch is a reserved word : https://developer.mozilla.org/en-US/...Reserved_Words

so you are probably throwing an error. Also, it's always a good idea to declare a proper html doctype and basic html page format for even test pages, jsut fyi.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
Tacitus86 (03-13-2013)
Old 03-13-2013, 03:45 PM   PM User | #4
Tacitus86
New to the CF scene

 
Join Date: Mar 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Tacitus86 is an unknown quantity at this point
Thanks for the information! I should have known that from regular java! But what error console are you guys using? I'm just coding in a text editor at the moment. Is there a good IDE for javascript editing?
Tacitus86 is offline   Reply With Quote
Old 03-13-2013, 03:54 PM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I don't use an IDE, but I write in notepad++ and tend to use the error console in Chrome (Tools>JavaScript Console) for basic stuff and firefox's firebug add-on if things get stickier
xelawho is offline   Reply With Quote
Old 03-13-2013, 04:17 PM   PM User | #6
Philip M
Supreme Master coder!

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

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 offline   Reply With Quote
Old 03-13-2013, 04:21 PM   PM User | #7
Tacitus86
New to the CF scene

 
Join Date: Mar 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Tacitus86 is an unknown quantity at this point
Thanks for the info Phil. As I mentioned in my OP, I am aware of the overwriting functionality of document.write. I was NOT aware that it was deprecated. Thank you!
Tacitus86 is offline   Reply With Quote
Old 03-13-2013, 04:28 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I don't know that it is actually deprecated, it's just that there are better ways of doing things now. document.write can still be useful in very specific circumstances, but nothing so basic as getting text to display on a page.
xelawho is offline   Reply With Quote
Old 03-13-2013, 04:43 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by xelawho View Post
I don't know that it is actually deprecated, it's just that there are better ways of doing things now. document.write can still be useful in very specific circumstances, but nothing so basic as getting text to display on a page.
I did say it was obsolete, not deprecated, but it is most certainly deprecated by felgall!
__________________

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 offline   Reply With Quote
Old 03-13-2013, 04:55 PM   PM User | #10
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by Philip M View Post
it is most certainly deprecated by felgall!
not entirely, but he comes quite close...
http://javascriptexample.net/badjs02.php
xelawho 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 02:31 AM.


Advertisement
Log in to turn off these ads.