Welcome to WebmasterWorld Guest from 54.224.22.27

Forum Moderators: open

Message Too Old, No Replies

Javascript not running with HTML code

     
9:39 pm on Oct 31, 2012 (gmt 0)

New User

joined:Oct 31, 2012
posts:9
votes: 0


Hello to all web loverzzz.
had a tiny little problem concerning this code.

HTML Code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>The American Eggplant Society</title>
</head>
<body>
<h1>The American Eggplant Society</h1>
<p>Welcome to our site. Unfortunately, it is still
under construction.</p>
<p>We last worked on it on this date:
<script type="text/javascript">

document.write(document.lastModified);

</script>
</p>
</body>
</html>

Been using Mozilla along with IE8 and chrome but cant figure out why scripts are not running at all :P
9:59 pm on Oct 31, 2012 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 970
votes: 23


Hi there alleria21,

try it like this...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>The American Eggplant Society</title>

<script type="text/javascript">
function init(){
document.getElementById('last-modified').firstChild.nodeValue=document.lastModified;
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>

</head>
<body>

<h1>The American Eggplant Society</h1>
<p>
Welcome to our site. Unfortunately, it is still
under construction.
</p><p>
We last worked on it on this date:
<span id="last-modified">&nbsp;</span>
</p>

</body>
</html>


birdbrain
10:23 pm on Oct 31, 2012 (gmt 0)

New User

joined:Oct 31, 2012
posts:9
votes: 0


Thanks birdbrain! works like a charm :P
cant imagine what went wrong with previous code though :/ and the one i posted earlier with the CSS im thinking it might be an issue with windows version or bit system maybe :P
11:12 pm on Oct 31, 2012 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 970
votes: 23


No problem, you're very welcome. ;)



birdbrain
2:59 am on Nov 1, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


document.write does not work with XHTML. The bigger question you might want to ask yourself is should I be using XHTML? For 99% of developers, the answer is no. See this old thread:
[webmasterworld.com...]
2:11 pm on Nov 1, 2012 (gmt 0)

New User

joined:Oct 31, 2012
posts:9
votes: 0


hmm it seems that xhtml has compatibility problems ...
Well then is it ok if i stick to HTML 4.01 Strict cuz im a little afraid of what are the advantages/disadvantages of doing so .but if todays developers use HTML 4.01 (at least till HTML 5 takes on) then it is ok :P
2:18 pm on Nov 1, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10551
votes: 10


i would suggest using the HTML5 DTD and HTML 4.01 compatible markup.
2:49 pm on Nov 1, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


Yeah, use an HTML 5 DOCTYPE, even if you don't use any of the new HTML 5 features. HTML 5 is a superset of HTML 4.01, so any valid HTML 4.01 document can use an HTML 5 doctype. Also, the HTML 5 DOCTYPE is so much easier to remember. :)
<!DOCTYPE html>
2:54 pm on Nov 1, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


Also, avoid using document.write, as it's generally considered bad practice to use it. Instead, use DOM methods to modify values and/or insert new content. birdbrain's example above uses the nodeValue to change the value of an existing node. Other options could include creating a new node and using appendChild or replaceChild to add it to the document.
5:08 pm on Nov 1, 2012 (gmt 0)

New User

joined:Oct 31, 2012
posts:9
votes: 0


:D Okay from now on i forget about xhtml :Pp thanks so much guys!