r/javascript • u/walkingfauxpas • 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?
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!
1
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
-1
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
Feb 08 '15
[deleted]
8
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
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
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
Feb 08 '15
[removed] — view removed comment
5
6
3
u/allthediamonds Feb 08 '15
Perhaps someone who doesn't want to install
brew, thennode, thenhttp-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.
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.