Advanced Search

Results 1 to 4 of 4

Thread: problem with variables and array in javascript. help out please

  1. #1
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face problem with variables and array in javascript. help out please

    Hi guys. Anytime I try to insert in a javascript code a variable or an array where I use if statements it doesn't display in my browser despite the document.write() method. Can anyone help out?
    I am using opera and mozilla.
    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>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Gosselin Laboratories</title>
        <link rel="stylesheet" type="text/css" href="physics.css" />
    <script type="text/javascript">
    var today= new Date();
    var curDay= today.getDay();
    if (curDay==0)
    	document.write("today is sunday.");
    else if(curDay==1)
    	document.write("today is monday);
    else if(curDay==2)
    	document.write("today is tuesday);
    else if(curDay==3)
    	document.write("today is wednesday);
    else if(curDay==4)
    	document.write("today is thursday);
    else if(curDay==5)
    	document.write("today is friday);
    else if(curDay==6)
    	document.write("today is saturday);
    </script>
    </head>
    </html>
    Last edited by james438; 03-17-2013 at 08:41 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    672
    Thanks
    1
    Thanked 110 Times in 108 Posts

    Default

    Hi there daniel curry,

    and a warm welcome to these forums.

    The use of "document.write()" is, generally, frowned upon.

    Here is a possible alternative for you to try...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>Gosselin Laboratories</title>
    
    <link rel="stylesheet" type="text/css" href="physics.css">
    
    <script type="text/javascript">
    (function() {
       'use strict';
    
    function init(){
    
       var dy=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
       var p=document.createElement('p');
       var t=document.createTextNode('Today is '+dy[new Date().getDay()]);
    
       p.appendChild(t);
       document.getElementById('container').appendChild(p);
     }
    
       window.addEventListener?
       window.addEventListener('load',init,false):
       window.attachEvent('onload',init);
    
    })();
    </script>
    
    </head>
    <body>
    
    <div id="container"></div>
    
    </body>
    </html> 
    Edit: The cause of the problem was those unclosed strings of yours like this - "today is monday being the first


    coothead
    Last edited by coothead; 03-16-2013 at 12:51 PM. Reason: forgot to mention the string problem. !!

  3. #3
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you Coothead for your help. it is working now. thanks a lot.

  4. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    672
    Thanks
    1
    Thanked 110 Times in 108 Posts

    Default

    No problem, you're very welcome.


    coothead

Similar Threads

  1. How can I set parts of this array to variables?
    By brainbuzzmedia in forum PHP
    Replies: 1
    Last Post: 10-28-2011, 07:00 AM
  2. php variables within javascript
    By liamallan in forum PHP
    Replies: 5
    Last Post: 07-22-2011, 02:53 AM
  3. Javascript to sum variables
    By Simora in forum JavaScript
    Replies: 2
    Last Post: 09-05-2009, 09:59 PM
  4. check variables against array
    By AdrielGreene in forum PHP
    Replies: 3
    Last Post: 07-26-2009, 07:21 AM
  5. Replies: 2
    Last Post: 03-17-2009, 07:09 PM

Bookmarks

Posting Permissions

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