Jump to content
Saeed

Does JS need HTML to run?

Recommended Posts

Good Day,Is it imperative to embed a JS script in an HTML page in order to execute the code? or is it possible to somehow run a script the way one would do a Java code from the command line?

Share this post


Link to post
Share on other sites

Did you try it? :)The W3C requires browsers to be VERY flexible and forgiving. They should try to render documents with no HTML or incorrect HTML as best they can. As long as you have <script> tags, code written between them should execute.

Share this post


Link to post
Share on other sites

You can also execute JavaScript in the URL of any page. The code will run as if it's part of the current page.For example, type the following in your browser:

java script:alert('There are '+document.getElementsByTagName('div').length+' "div" elements on this page.');

(NOTE: Remove the space between "java" and "script". The forum adds it for security reasons)As long as the code is one statement (possibly with closures... long story), you can do that.You can also open your browser's console and type there. On IE, press F12. Same on FF, but if you have Firebug installed.

Share this post


Link to post
Share on other sites

I test javascript snippets in my console all the time. I find it especially useful for testing regular expressions, because it's very easy (fast) to make small changes.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...