A very basic Javascript problem!
Posted: Wed Sep 08, 2010 11:11 am
Hi,
I don't use Javascript much (and am a complete duffer with it!) but I have a little test function which works fine when embedded in the html itself. However, as soon as I move the routine to an external .js file the damn thing will not load. I get a 'Invalid character... line 1... character 1' error. I even get this error when linking to an empty .js file! I've tried messing around with the EOL characters and BOMs etc. but all to no avail.
Anyone know why this might be happening?
Here's the HTML :
And here's the JS :
The routine simply collapses/expands a HTML div etc. As I say, works fine if embedded directly in the HTML.
Thanks.
I don't use Javascript much (and am a complete duffer with it!) but I have a little test function which works fine when embedded in the html itself. However, as soon as I move the routine to an external .js file the damn thing will not load. I get a 'Invalid character... line 1... character 1' error. I even get this error when linking to an empty .js file! I've tried messing around with the EOL characters and BOMs etc. but all to no avail.
Anyone know why this might be happening?
Here's the HTML :
Code: Select all
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>New Page 1</title>
<script language="JavaScript" src="test.js"></script>
</head>
<body>
<span onmousedown="toggleDiv('mydiv');"><img id="mydiv_img" src="collapsed.gif"></img></span>
<div id="mydiv" style="display:none">This is a test!<br>Heyho!</div>
<p>Hello!</p>
</body>
</html>
Code: Select all
function toggleDiv(divid){
if(document.getElementById(divid).style.display == 'none'){
document.getElementById(divid).style.display = 'inline';
document.getElementById(divid + '_img').src = 'expanded.gif';
}else{
document.getElementById(divid).style.display = 'none';
document.getElementById(divid + '_img').src = 'collapsed.gif';
}
}
Thanks.