Connecting Tech Pros Worldwide Forums | Help | Site Map

Using JavaScript with Dreamweaver:

Newbie
 
Join Date: May 2007
Posts: 1
#1: May 15 '07
If i run my javascript withen html using dreamweaver, it works perfectly well. But when i try to use 'src' command to run the script from another file it doesnt work.

Can anybody tell me why. and what i can do to correct it.

Java script withen html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My first script</title>
</head>
<body bgcolor="#FFFFFF">
<h1>
<script language="javascript" type="text/javascript"> document.write("Hello, world!");
</script>
</h1>
</body>
</html>

Javascript with src command


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>second script</title>
<script language="Javascript" type="text/javascript" src="script.js">
</script>
</head>

<body bgcolor="#FFFFFF">
<h1 id="helloMessage">
</h1>
</body>
</html>


script:

// JavaScript Document
window.onload = writeMessage;
function writerMessage() {
document.getElementById("helloMessage").innerHTML = "Hello, world";
}

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: May 16 '07

re: Using JavaScript with Dreamweaver:


compare the following working code to yours ... have a look at the doctype definition and the typo in your script-source

[HTML]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>second script</title>
<script language="Javascript" type="text/javascript" src="script.js"></script>
</head>

<body bgcolor="#FFFFFF">
<h1 id="helloMessage"></h1>
</body>
</html>[/HTML]

Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2. window.onload = writeMessage;
  3.  
  4. // you had a typo here!
  5. function writeMessage() {
  6.     document.getElementById("helloMessage").innerHTML = "Hello, world";
  7. }
... if you are starting to use javascript i would recommend to test your code with firefox and the firebug-extension ... gives you really good hints on errors you make ... for example it would say that method writeMessage is not defined with your code ... until you wrote writerMessage ... and thats a hint for you to have a look why :) ... so you may find such things real fast ...

kind regards ...
Reply