www.webdeveloper.com
Results 1 to 5 of 5

Thread: Can not see javascript?

  1. #1
    Join Date
    May 2013
    Location
    Louisville KY
    Posts
    2

    Can not see javascript?

    I am new to javascript and am building my first website, after having the content of the website typed up and styled with html and css, I wanted to practice writing a simple javascript code to it. The purpose of the javascript is to repeat on all the pages the copyright information for my website. (the simplest of all codes I believe....) anyways, I post this code to my <div id="footer"> section.

    <script type="text/javascript"
    document.write(copyright);
    </script>

    I also have the page linked to the external .js file. This files holds ...
    <script type="text/javascript">
    var copyright='blah blah blah';
    document.write(copyright);
    </script>

    Now when ever i reload Firefox to view the change to make sure the code works, my footer section of the page that is supposed to hold the copyright is blank... Its almost as if the code worked.. But it isn't displaying, I checked the settings, and yes javascript is enable on my browser, I can go and visit javascript enabled websites and view everything just fine.
    Any ideas on why the code is not displaying my information?
    Thanks!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,352

    Lightbulb

    You can not use "document.write('anything')" after the page has been rendered.

    If you do, the page is reloaded.

    Use a <footer id="footerTag"></footer> in the body
    and then, just before the </body> tag place a script
    <script type="text/javascript">
    var copyright='blah blah blah';
    document.getElementById('footerTag').innerHTML = copyright;
    </script>

  3. #3
    Join Date
    May 2013
    Location
    Louisville KY
    Posts
    2
    I'm afraid I do not understand. If I am correct <footer></footer> is html5, the webpage I am building I have a doctype of 4.01 transitional. What I am wanting to do is a repeating variable, I think. I read about it in a javascript tutorial. And from what it explained, you can make an external JS file with a var equaling to a string of text, having it be what ever you want. Then all you have to do is place document.write(var name) into where you want to display that text in your page. The tutorial mentioned nothing of the footer id and getelementByID that you mentioned. I want to do this because I have the copyright information at the bottom of every page, and if I want to change it in the future, I do not want to have to go to every page and change it, I want to be able to change that one JS file so that then, every page information is changed automatically.
    Thank you for the reply by the way. Do you think you could clarify a little more?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,352

    Lightbulb

    So instead of the <footer> tag pair, use a <div> tag pair instead.

    Then create an external file named, for example, that contains your copyright information:
    footerInfo.js
    Code:
    var copyrightedby = 'tsidebottom10';
    Then consistently give an id= for each page you wish the footer to be displayed, as in:
    Code:
    <!doctype stuff...>
    <html>
    <head>
    <title> Whatever </title>
    <script type="text/javascript" src="footerInfo.js"></script>
    </head>
    <body>
    ...
    
    <div id="footerTag"></div>
    <script type="text/javascript">
    window.onload = function() { document.getElementById('footerTag').innerHTML = copyrightedby; }
    </script>
    </body>
    </html>

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    In this case, it's possible to use document.write (the document is not closed).
    But you have at first to load the script which define the variable copyright (a HTML string).

    An example of simple script copyrigth.js (which update the year).
    Code:
    // JavaScript Document
    var copyright="<p>&copy; "+new Date().getFullYear()+"</p>";
    Then, your script should be placed in the footer with something like this :

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <!-- Load the script -->
    <script type="text/javascript" src='copyright.js'></script>
    <style type="text/css">
    #footer {font-size:small;text-align:center;}
    </style>
    </head>
    <body>
    	<div>
    		<p>Good luck</p>
    	<div>
    	<div id="footer">
    <!-- Insert the variable copyright in the footer -->
    <script type="text/javascript">
    	document.write(copyright);
    </script>
    	</div>
    </body>
    </html>
    Last edited by 007Julien; 05-12-2013 at 06:11 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles