Newbie Javascript issue. Works when inline but not when using src=

Discussion in 'Web Design and Development' started by blodwyn, Feb 22, 2014.

  1. blodwyn macrumors 65816

    Joined:
    Jul 28, 2004
    Location:
    Portland, Oregon
    #1
    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
     
  2. blodwyn thread starter macrumors 65816

    Joined:
    Jul 28, 2004
    Location:
    Portland, Oregon
    #2
    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
     
  3. tebruno99 macrumors newbie

    Joined:
    Mar 16, 2012
    #3

    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.
     
  4. YanniDepp macrumors 6502

    YanniDepp

    Joined:
    Dec 10, 2008
    #4
    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.
     
  5. blodwyn thread starter macrumors 65816

    Joined:
    Jul 28, 2004
    Location:
    Portland, Oregon
    #5
    Ah, thanks for the info
     

Share This Page