0

I created a simple HTML file using TextEdit, set to plain text UTF-8 (on a Mid 2012 MacBook Pro, running Sierra). Markup as follows:

<html>
    <head>
        <title>Simple Page</title>
    </head>

    <body>
        <p>This is a very simple HTML page</p>
        <script src="script.js"></script>
    </body>
</html>

The linked JavaScript file is in the same directory as the HTML document and is also written in TextEdit and contains:

alert(“Hello world”);

However, when I opened that local HTML file in several browsers none of them showed my pop-up despite the fact that pop-UPS ARE enabled in both browsers. So, upon that I inspected my code. The console error I'm getting is:

Uncaught SyntaxError: Invalid or unexpected token                                 script.js:1

I found that my script had been interpreted as:

alert(“Hello worldâ€);

Which is obviously not what I wrote, so something is going wrong somewhere. I've spent hours trying to figure out why my code isn't being interpreted correctly. Can anyone tell me why my code file is ending up with such strange characters, and thus not running?

Pia
  • 51
  • 2
  • 3
    Looks like you're trying to use characters which aren't quotes. Use quotes. – David Oct 02 '17 at 14:18
  • 1
    Try using Notepad++ – scrappedcola Oct 02 '17 at 14:18
  • 3
    How did you write this code? Did you use a proper text editor like `notepad++` or `sublime text` ? – ProEvilz Oct 02 '17 at 14:18
  • javascript code in an HTML file must be wrapped in a `` tag. Also yes, use a text editor. – user2954463 Oct 02 '17 at 14:18
  • 5
    Make sure your text editor isn't using `“` (fancy double quotes) – evolutionxbox Oct 02 '17 at 14:19
  • You can also you ' instead of ", they may be encoded differently – Surreal Oct 02 '17 at 14:22
  • 1
    If you edit in the _exact_ code you used for your JavaScript file I'll vote to reopen this post. – gunr2171 Oct 02 '17 at 14:35
  • To closers: while it certainly would have been preferable to have inline code instead of links to screenshots that could have been edited in. The question is clear: the OP even figured out part of the problem. Voting to reopen. – Jared Smith Oct 02 '17 at 14:35
  • 4
    To @JaredSmith: while it's clear what OP is trying to do, the question was not closed as "Unclear". The MCVE close reason specifies you must include your code in the question itself. We cannot debug pictures. The question can still be edited while closed and can be reopened if OP includes an MCVE. Reopening before then sends a signal to the OP that it's OK to keep posting screenshots in lieu of code. – TylerH Oct 02 '17 at 14:40
  • 4
    Please add code, errors and data as **text** ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](https://meta.stackoverflow.com/a/285557). In general, code/errors/data in text format >>>> code/errors/data as an image >> nothing. Images should only be used, *in addition to text in code format*, if having the image adds something significant that is not conveyed by just the text code/error/data. – Makyen Oct 02 '17 at 14:41
  • @TylerH fair enough. I still think that we are over-applying the letter of the law, but your point is cogent. – Jared Smith Oct 02 '17 at 14:42
  • @Makyen I absolutely understand what you're saying and I'm really sorry I didn't format my question correctly.To be honest this is my first ever question and, as mentioned, I have pretty much 0 knowledge when it comes to any of this, meaning that I don't even get most of the instructions in the "how to frame questions" section. Nevertheless, I suppose I will try to edit my question to get it closer to how it ought to be. Thank you all, nevertheless, and I hope you're able to help me out once I get the hang of things – Pia Oct 02 '17 at 18:41
  • @Pia, I look forward to seeing your edited question with code in text format. [Note: I haven't voted yet on your question, I usually prefer to give a chance for improvement.] Part of the issue is that the primary goal of SO is to make a repository of Q&A which are valuable to future readers, not just the original person asking (OP). Having code only in images makes the question basically useless to anyone other than the OP (i.e. difficult for anyone with a similar issue to find and use). – Makyen Oct 02 '17 at 19:01
  • In order to give you a specific answer for the environment in which you're working, we're going to need to know the OS you are running and the editor you're using. The general advice which has already been given of using an editor intended for editing text/code is valid. – Makyen Oct 02 '17 at 19:01
  • @Makyen thanks for the info. I have done my best to format my question better and hope it is more of a valuable and useful one now – Pia Oct 02 '17 at 19:13
  • It's better. However, you are also getting a specific error in the console, the text for which should be included in the question. Having that information is significantly more specific than just "it doesn't show the `alert()`". Without it, one of the first questions people will ask is "what does it show in the console?" Given that you're using TextEdit, should we assume you're on a Mac? TextEdit tends to be common on Macs, but can be used under most operating systems. – Makyen Oct 02 '17 at 19:35
  • @Makyen yes, it's a Mid 2012 Mac Book Pro that I am using (running Sierra) and regarding the error, I honestly don't know how to be more specific, as the pop-up window simply doesn't show. In this case, would it be okay to include a screenshot to help me demonstrate? – Pia Oct 02 '17 at 19:38
  • @Pia You had a screenshot in your original question which showed an error in the console. [I've circled it here](https://i.stack.imgur.com/7UbyZ.png). In general, images are beneficial if they provide supplemental information which can't be conveyed easily in text (i.e. is long/verbose/unclear in text). Error messages are only *very* rarely something that can not easily be shown as text. This is not one of those situations. – Makyen Oct 02 '17 at 20:00
  • @Pia, For the `alert("Hello world");` which you included in this question, did you copy-&-paste it from your editor, or did you retype it into the question? It would be better if you copy-&-paste the text from your editor, as exactly what's in your editor matters in this case. – Makyen Oct 02 '17 at 20:41
  • @Makyen I retyped it, but will go in and edit it & copy paste instead – Pia Oct 02 '17 at 20:48
  • Additional related/duplicate: [MacOSX: how to disable accented characters input](https://stackoverflow.com/a/12759063), [C not recognizing double quotes on Mac OSX](https://stackoverflow.com/q/21889349), [Cocoapods install getting error](https://stackoverflow.com/a/21404604), and [Error using Pod Install command on Podfile in Terminal](https://stackoverflow.com/q/31832538) – Makyen Oct 02 '17 at 20:59
  • 1
    ©Makyen Thank you a million times over, this https://stackoverflow.com/a/12759063/8708811 solved the issue (will edit link into question for others to see) :) – Pia Oct 03 '17 at 08:54

0 Answers0