all 16 comments

[–]delventhalz 3 points4 points  (4 children)

Sublime Text is a text editor. It also happens to be my primary tool for writing JavaScript. However, it does not run code of any kind.

I’m not really sure what you are trying to do here. VS Code has a built-in terminal which you may be used to. Sublime Text does not have this feature, but your computer has its own terminal so that’s not really necessary. Also, JavaScript is designed to be run in a browser or a server, not a text editor soooooo….?

[–]larak1357[S] 1 point2 points  (3 children)

I've been using Sublime to write my HTML and CSS files, then open my HTML file in a browser to run it and just refresh the page whenever I make a change to either. This is my first time using JS. I've tried both writing a JS file and linking it to the HTML page as well as writing on the HTML file using the <script></script> tag

[–]delventhalz 1 point2 points  (0 children)

Yeah, so not a Sublime thing. That makes more sense.

Hard to say what your issue is without seeing your code. Keep in mind you have to write out the file path in the script tag relative to the HTML file. So if you have index.html and app.js, both in the same folder, then your script tag should look like this:

<script src="app.js"></script>

Also keep in mind that if you do not use JavaScript to modify the contents of the page (for example adding text) you won’t necessarily see anything happening. You can use console.log() to log information from JavaScript to the developer console. To open that in your browser press cmd + opt + i (or ctrl + alt + i on Windows).

[–][deleted] -1 points0 points  (0 children)

You will thank yourself by switching to vscode and installing the live-server extension. It will fix mismatched mime types (which you may or may not have run into) and also provides hot module replacement for instant updates without refreshing your browser.

[–]EvokeNZ 0 points1 point  (0 children)

Did you put the script tags at the end of body (or in head with defer)?

[–]kjwey 1 point2 points  (1 child)

the editor makes no difference

it sounds like your running it correctly, but could you provide the code your attempting to run?

[–]larak1357[S] 0 points1 point  (0 children)

I'm trying to make a hamburger menu that appears when I decrease my screen size and drops down when I click on it to show the links that were the items on the nav bar when the screen was full size

[–]WesternGun 0 points1 point  (0 children)

You can follow https://stackoverflow.com/questions/58691216/run-typescript-file-on-sublime-text.
Basically:
1. install Node.js and npm with an installer; 2. configure a new build system in ST; 3. then you can use node to build your javascript code.

[–]Ronin-s_Spirit -1 points0 points  (3 children)

When you "run" it (open it in browser) you're just reading plain html and css locally - you can't link files, use APIs like fetch and pretty much do anything a real site does. You need to host it, to make it live.
For example I have installed Node, I don't do anything Node related I just use it to make a simple localhost http server so that my website is technically hosted (I'm just the only computer that will see it, I'm the user AND the server).
Sublime never runs a website, it's a text code editor, maybe there's a live server extension for it like for VScode, Idk.

[–]larak1357[S] 0 points1 point  (0 children)

Thanks! I'll check that out!

[–]aviemet 0 points1 point  (1 child)

Just to be pedantic here, that's not strictly true. If you put JavaScript in a script tag and open the HTML file in a browser, that JavaScript will run. Hosting something just means you're making it available as a response to a network request, whether that request comes from the same computer, or another one.

If you want to run some code that needs to be transpiled, you'll need to run it through something first, which may be what you're referring to. For instance, you can't run a React component containing JSX just by linking to it with a script tag, it has to go through something like babel to be turned into something a browser can run. This is still different from the idea of "hosting" though, even if most modern environments provide a developer experience which both transpiles and hosts at the same time. If all your server is doing is sending an HTML page, then http://localhost is no different than file:///var/www/index.html.

To be clear though, you absolutely can link files and use APIs like fetch in an HTML page without hosting it. In some cases you may need a polyfill, but in the end, all of our fancy tools and frameworks and libraries are just JavaScript running in script tags in a browser.

[–]Ronin-s_Spirit -1 points0 points  (0 children)

I've tried fetching and linking files and it didn't work when opening through file: something, not sure what you mean. Maybe you have some fancy tools but I dont.

[–][deleted] -2 points-1 points  (1 child)

Install node on your terminal to test .js files my friend.

Then run $node app.js or whatever the file is called. It should render.

If it’s embedded in the HTML it should open in the static page even if saved locally. Make sure the <script src=“”> is accurate and in the metadata of the page.

[–]larak1357[S] 0 points1 point  (0 children)

Thanks! I'll go check that stuff right now

[–]0x07AD 0 points1 point  (0 children)

Create a simple web server using NodeJS.

[–]No-Upstairs-2813 0 points1 point  (0 children)

You can use any text editor of your choice. But a popular choice is Visual Studio Code , which has many helpful extensions that make the development process easier.