Log in

View Full Version : problem with variables and array in javascript. help out please



daniel curry
03-16-2013, 09:27 AM
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.:o

<!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>

coothead
03-16-2013, 12:42 PM
Hi there daniel curry,

and a warm welcome to these forums. ;)

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

Here is a possible alternative for you to try...


<!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>

The cause of the problem was those unclosed strings of yours like this - "today is monday being the first ;)

coothead

daniel curry
03-17-2013, 05:30 PM
thank you Coothead for your help. it is working now. thanks a lot.

coothead
03-17-2013, 08:15 PM
No problem, you're very welcome. ;)


coothead