最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is an ".html" file necessary when I use JavaScript? - Stack Overflow

programmeradmin0浏览0评论

If I understood correctly, JavaScript serves to interact with the DOM of an HTML file. So, does a .js file always have to be linked to an .html file?

To execute JavaScript code, do I always need to have an .html file?

If I understood correctly, JavaScript serves to interact with the DOM of an HTML file. So, does a .js file always have to be linked to an .html file?

To execute JavaScript code, do I always need to have an .html file?

Share Improve this question edited May 4, 2018 at 19:25 Ivar 6,90712 gold badges56 silver badges67 bronze badges asked May 4, 2018 at 19:19 correocontcorreocont 697 bronze badges 4
  • 4 This is not a well-scoped StackOverflow question ... but generally, you only need an HTML file if you're serving Javascript to a browser. With Node.js you can run Javascript on the server, which means you don't need an HTML file. You have to define your use case a little more clearly for anyone to give you better guidance. – user Commented May 4, 2018 at 19:21
  • 1 Sure. You can run JS in a console without the need for html or you can use it as a server side language with node.js. There are other examples but these are the most mon instances you'd encounter – chevybow Commented May 4, 2018 at 19:22
  • No, if you are running your application in browser then you will need .html file but if you are using JavaScript in any other environment like node then you don’t need to use .html file. So, simple answer is you can run .js file without .html – Sandip Nirmal Commented May 4, 2018 at 19:22
  • 2 @user it's broad. No question. Too broad? I don't think so, YMMV. – Jared Smith Commented May 4, 2018 at 19:34
Add a ment  | 

5 Answers 5

Reset to default 4

Well, an .html file does not execute JavaScript. A browser will render HTML, and in doing so interpret and execute any JavaScript it encounters.

Really, it is the browser that is necessary for JavaScript to execute in the context of HTML. That said, all you really need is the interpreter. These can be found in browsers, for example the dev console is an easy way to execute JavaScript without html. You can also just use javascript:alert('hello world'); as a url in the browser and watch the script execute.

You can also find interpreters server side, in the form of the popular node.js. Node will interpret and execute the JavaScript without any html or browser needed.

In addition to all of this, any other source of interpreter can be used, some are custom written, so long as they execute the language's syntax.

How "interpreted" languages like JavaScript usually work:

A piler takes your source code and transforms it on the fly into something else. That something else (could be a lot of different things, usually bytecode) is then converted by the underlying runtime into machine code for the specific puter architecture (e.g. x86, ARM).

I'm glossing over a lot of details and edge-cases here, but the gist is correct. So you need two things, the piler and the runtime which in practice are almost always bundled together.

In the case of JavaScript, the piler/runtime is embedded in a web browser and initiated through interaction with an HTML document. There are other JavaScript runtimes like node and rhino that do not use an HTML document/Web Browser as the "host" platform.

So (in theory) to execute JavaScript code you just need a standards-pliant piler/runtime. In practice it's more plicated.

The "script" in JavaScript means that JavaScript is a scripting language. That term gets used somewhat promiscuously but it means "a language meant to be embedded to extend and modify a host environment". In this case, JavaScript is the scripting language of HTML documents. Most JavaScript even in today's post node.js world is written with the assumption that it will execute in the context of an HTML document although conscientious developers are getting better about writing portable JavaScript.

So your average snippet from, say, a StackOverflow answer may work elsewhere, but probably not.

You can run JS on chrome' s console for example, or you can use Node without an HTML file to run javascript code. You don' t always need an HTML file.

No.

You can use JS as a backend language, by using Node.js for example.

If you need to run Javascript code for testing/learning, you can install NodeJS, create .js files and run them using the mand line for example.

You only need to "link" it to html when you need to use it in a webpage to manipulate the DOM for example

发布评论

评论列表(0)

  1. 暂无评论