Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

blodwyn

macrumors 65816
Original poster
Jul 28, 2004
1,147
1
Portland, Oregon
Hi, I'm sure this is the dumbest question ever, but I'm just getting started with Javascript and I'm baffled.

This code works fine, alert box pops up as expected:

<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<p>This is a simple HTML page</p>
<script>alert("hello world")</script>
</body>
</html>

However if I strip out the Javascript into a separate file "script.js" then the alert box does not pop up. Here's the code for the html:

<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<p>This is a simple HTML page</p>
<script src="script.js"></script>
</body>
</html>

and here's the contents of script.js:

alert(“Hello World“);

According to the course on Lynda.com that I'm trialling, this should work. However it doesn't work either on a website, or if the files are on my desktop and opened as local files. Tried Safari, Firefox, and Chromium with no luck, also no errors reported in Firebug that I can see.

Using Mavericks and BBEdit to generate the files. Code works when inline so I know the Javascript is actually enabled in the browser.

TIA for any pointers

Edit: Hmmm, apologies that the code indents don't seem to survive when I save the post
 
No worries, I found the issue

For some reason the wrong type of double quote character was inserted in the alert statement in the script.js file (“ instead of "). Don't know how they got there as they don't even appear on my keyboard.

Thanks anyway
 
For some reason the wrong type of double quote character was inserted in the alert statement in the script.js file (“ instead of "). Don't know how they got there as they don't even appear on my keyboard.

Thanks anyway


This can happen when copy and pasting from an OSX "styled" text box like in iChat/iMessage to another source. Its very hit and miss I find but when using coding applications (xchat, textmate, nano) it usually doesn't happen.
 
In older versions of OS X, it was a feature built in to a few apps (like Pages). In Mavericks, it's now a system-wide thing (except in things like Terminal).

To disable it, go to the Edit menu, then Substitutions, and disable Smart Quotes.
 
In older versions of OS X, it was a feature built in to a few apps (like Pages). In Mavericks, it's now a system-wide thing (except in things like Terminal).

To disable it, go to the Edit menu, then Substitutions, and disable Smart Quotes.

Ah, thanks for the info
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.