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

    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!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    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

  • Users who have thanked xelawho for this post:

    Tacitus86 (03-13-2013)

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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.

  • Users who have thanked DanInMa for this post:

    Tacitus86 (03-13-2013)

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    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.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    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


  •  

    Posting Permissions

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