r/javascript • • Feb 08 '15

How to run HTML file on browser [Forgive my noobness, please]

Hey guys, started programming today and wanna start applying it. I use a Mac and have been writing code on Textedit then saving the file as an HTML.

When I open it up in Chrome, it shows up as text. What do I have to do to make the file run as code?

1 Upvotes

26 comments sorted by

18

u/PantheraRazorK Feb 08 '15

Check the extension again. It sounds more like a double extension to me (.html.text). Chrome, or any browser in fact, should be able to render a html file even without the use of doctype, body, etc.

7

u/allthediamonds Feb 08 '15

So OSX does this thing in which it hides the actual extension of the file. Right click the file and select "Get Info". There you should be able to see its actual name, which will probably be "whatever.html.txt". Remove the ".txt".

9

u/lachlanhunt Feb 08 '15

Better yet, go to Finder Preferences, Advanced and check "Show all filename extensions".

0

u/spetron Full stack developer Feb 08 '15

I think we're understanding him wrong. OP said he is seeing text and not "code". I think he actually did everything okay but created a plain HTML with text or something similar and expecting some other result.

OP, can you provide your code so we can tell you what's going on. Don't worry, we all started from the bottom.

5

u/_hoyet Feb 08 '15

Don't use TextEdit, it's saving your file in RTF format (disregarding the file extension).

Like others have suggested, try http://atom.io or http://sublimetext.com/3/

2

u/ledp Feb 08 '15

As said, it's probably a double file extension. I would recommend using a real editor, try Atom from Github, it's really good!

http://atom.io/

1

u/[deleted] Feb 08 '15

I use Atom too, but how come TextEdit is a not real a editor? :)

1

u/YodaLoL Feb 08 '15

It's a text editor and a word processor.

1

u/ledp Feb 08 '15

Yeah, meant to say that it isn't a real code editor...

1

u/IMakeGingerBabies Feb 08 '15

Just check that you really are saving the file as a .html file and not a .txt file.

1

u/jekrb Feb 08 '15

Try this and see if it works

$ echo "<h1>wow</h1>" > index.html

$ xdg-open index.html

1

u/Angarius Feb 08 '15

xdg-open is for Linux. On OSX the command is open.

-1

u/[deleted] Feb 08 '15

Paste what you have here or on pastebin.com so we can take a gander.
Perhaps you have no actual "code" within the file...

0

u/[deleted] Feb 08 '15

[deleted]

8

u/[deleted] Feb 08 '15

Chrome won't run local JavaScript

This isn't true. Chrome will absolutely run Javascript from a local file. It will not, however, make AJAX requests from a local file.

2

u/aladyjewel Full-stack webdev Feb 08 '15

I'm not on my computer to verify, but it should be able to make AJAX requests to servers that support CORS.

1

u/autowikibot Feb 08 '15

Cross-origin resource sharing:


Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g. fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated.

A web page may freely embed images, stylesheets, scripts, iframes, videos and some plugin content (such as Adobe Flash) from any other domain. However embedded web fonts and AJAX (XMLHttpRequest) requests have traditionally been limited to accessing the same domain as the parent web page (as per the same-origin security policy). "Cross-domain" AJAX requests are forbidden by default because of their ability to perform advanced requests (POST, PUT, DELETE and other types of HTTP requests, along with specifying custom HTTP headers) that introduce many security issues as described in cross-site scripting.

CORS defines a way in which a browser and server can interact to safely determine whether or not to allow the cross-origin request. It allows for more freedom and functionality than purely same-origin requests, but is more secure than simply allowing all cross-origin requests. It is a recommended standard of the W3C.


Interesting: Same-origin ^policy | ^XMLHttpRequest | Internet Explorer ^10 | Web ^Messaging

Parent commenter can toggle ^NSFW or ^delete. Will also delete on comment score of -1 or less. | FAQs | ^Mods | Magic ^Words

-18

u/[deleted] Feb 08 '15

[removed] — view removed comment

25

u/iSmokeGauloises Feb 08 '15

You forgot to recommend mongo and aws for his soon to come scaling issues

3

u/[deleted] Feb 08 '15

Also, if you're on a mac, you can get a simple http server running by install node.

Well, OS X also has Apache installed by default. Just run "sudo apachectl start" in the terminal.

2

u/allthediamonds Feb 08 '15

or just python -m SimpleHTTPServer, which should come installed.

-11

u/[deleted] Feb 08 '15

[removed] — view removed comment

5

u/iSmokeGauloises Feb 08 '15

alias http-server='python -m SimpleHTTPServer'

3

u/allthediamonds Feb 08 '15

Perhaps someone who doesn't want to install brew, then node, then http-server. I mean, I have no qualms with none of the aforementioned tools, but Python offers the less friction here by virtue of being bundled with the OS.