A very basic Javascript problem!

For everything that's not in any way related to PureBasic. General chat etc...
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

A very basic Javascript problem!

Post by srod »

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 :

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>
And here's the JS :

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';
  }
}
The routine simply collapses/expands a HTML div etc. As I say, works fine if embedded directly in the HTML.

Thanks.
I may look like a mule, but I'm not a complete ass.
User avatar
Innesoft
Enthusiast
Enthusiast
Posts: 105
Joined: Mon Jan 18, 2010 10:30 am
Location: UK
Contact:

Re: A very basic Javascript problem!

Post by Innesoft »

It works without a problem here, firebug gives no warnings or errors and I don't see anything wrong with the code itself. BOM/EOL shouldn't be a problem either way.

Tested in FF3, IE8

Try adding... divid.toString() + '_img'.. just to be sure, though you shouldn't need to, as string conversion is done on the fly.

Are you testing this locally or on a remote server? Which browser? Is it a browser error or an IDE error?

Also, this will help you a lot --> http://getfirebug.com/
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: A very basic Javascript problem!

Post by srod »

Thanks for the reply.

Using IE8 on Vista Premium. Testing locally only.

As I say, I get the same error if the .js file is empty!

Have tested with other .js files I downloaded... same problem. I can see nothing amis with the security settings etc.

Very strange for sure.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: A very basic Javascript problem!

Post by srod »

Well, I have changed nothing... but now it is working!

Doh!!! :)

What on Earth is going on here?
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: A very basic Javascript problem!

Post by Trond »

IE must have cached a bad version of your file.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: A very basic Javascript problem!

Post by srod »

Trond wrote:IE must have cached a bad version of your file.
Yes that makes sense... buggered if anything else does mind! :)
I may look like a mule, but I'm not a complete ass.
Post Reply