Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two files; new.html and new.js. But javascript does not work. When I add the content of new.js file into new.html (in either head or body tag) it works. When I place javascript code into a separate file, javascript code does not work.

What am I doing wrong?
XML
new.html

<html>
<head>
<script type="text/javascript" src="new.js"></script>
</head>
<body>

<div id="divId" onmouseover="Change();" onmouseout="ChangeBack();" > üstüne gel </div>
</body>
</html>

new.js

<script type="text/javascript">
function Change()
{
    document.getElementById("divId").innerHTML = "Ne koyim";
}
function ChangeBack()
{
    document.getElementById("divId").innerHTML = "Ne koyarsan koy";
}
</script>
Posted
Updated 29-Dec-10 8:41am
v3

Oh my, it took me a while until I spotted that one!
In your new.js file you can leave out the script tag. It should look like this:

JavaScript
function Change()
{
    document.getElementById("divId").innerHTML = "Ne koyim";
}
function ChangeBack()
{
    document.getElementById("divId").innerHTML = "Ne koyarsan koy";
}


If you delete the just the script tags, you'll be doing fine.
Depending on where you're storing the new.js file you may need to adapt the path in the script tags src attribute. It's good to make it a relative path:
XML
<script type="text/javascript" src="./new.js"></script>

The path above works great when the new.js file is in the same directory as the new.html


Regards,

Manfred
 
Share this answer
 
v4
If you are putting the javascript code in separate file then you need to include in your page. You can do this as
<script type="text/javascript" language="javascript" src="path of your js file"></script>

and can put it in head tag.
 
Share this answer
 
Comments
fjdiewornncalwe 29-Dec-10 15:57pm    
Why the 1 vote. @Brij: If you read the OP's code snippet and question, he already has done this and understands how to. His issue isn't directly related to what you say here.
Brij 29-Dec-10 22:58pm    
You are right.I missed that one :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900