2

I recently installed v8 in my system and was trying few javascript programs, but some of the output functions like alert, prompt etc. is not being recognised by it. The same program if i run it in browser is working fine. Am I missing something?

peSHIr
  • 6,279
  • 1
  • 34
  • 46
Rahul
  • 11,129
  • 17
  • 63
  • 76

3 Answers3

4

Yes: the functions you mention aren't actually part of the JS language specification, but a web-browser specific extension of it.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
4

alert and prompt are not part of the DOM. They are so-called host objects that "live" in the browsers.

The JavaScript spec defines native objects which are documented in the spec.

The browsers implement the JavaScript spec, but they also introduce additional host objects. The alert and prompt functions are examples of these.

The DOM is just a subset of all the host objects that exist in the browser. Specifically, the DOM is considered to be the document object and all its properties. The document object is just one of many host objects that "live" in the browsers. All other host objects (like alert and prompt) are not part of the DOM.

Update:

The DOM is defined by W3C. There are 11 DOM standards. The alert method is not defined in any of them, so it is not a DOM method. There are literally hundreds of browser objects that are not defined by DOM standards and those objects are not considered part of the DOM. Some people don't understand this distinction, so they think that all browser objects are DOM objects.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • so basically javascript is used to handle these things . so is there a way that i can use these host objects or even DOM objects using standalone javascript interpreter ? – Rahul Oct 14 '10 at 13:03
  • Host objects are defined by the environment in which the JavaScript code runs. In the browser, the browser is the environment. I am not sure in what environment your code runs. How did you install v8? How do you run it? – Šime Vidas Oct 14 '10 at 13:09
  • i followed this thread http://stackoverflow.com/questions/1802478/running-v8-javascript-engine-standalone – Rahul Oct 14 '10 at 13:15
  • So you are running JavaScript from the console. Then I assume there could be some custom functions defined which are used to print to the console (instead of alert)... Try to find the documentation of V8 – Šime Vidas Oct 14 '10 at 13:21
  • I intend to understand web browsers more profoundly , thus decided to coalsce these indivual units .Is there any way i can integrate this DOM API – Rahul Oct 14 '10 at 14:45
  • The DOM is a set of open standards. Each browser implements these standards to certain extends. You can how well the browsers confer to the standard here: http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Document_Object_Model) There might be a JavaScript library that simulates the DOM API, however, since the browsers don't implement the standard fully, it is not enough to know the standard, but you need also earn experience of how each browser implements the standard – Šime Vidas Oct 14 '10 at 15:29
1

alert() and prompt() are methods of the DOM, so you only have them in a browser environment.

For more information see The DOM and JavaScript on MDC.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • so using javascript we r accessing DOM methods ur saying ? if its true then is there any other language from which we can access these DOM methods ? – Rahul Oct 14 '10 at 12:39
  • Yep. The default namespace for browser-based JS is `window`. These functions are actually `window.alert()` and `window.prompt()`. – Ryan Kinal Oct 14 '10 at 12:40
  • Yes, @Rahul, the `alert()` function is `window.alert()`, and there's no `window` in a V8 program. (There's a global context, but it's not `window`, which is part of the **browser** not Javascript.) – Pointy Oct 14 '10 at 12:42
  • so basically javascript is one of the ways to use DOM methods which are embedded in browser , so if a browser is built in which interfaces are provided by a different language , lets say python then in that case can python be used as client side scripting ? Ps: sorry if i sound ridiculous , i am pretty new to these stuff – Rahul Oct 14 '10 at 12:45
  • @Šime Vidas Yes they are: https://developer.mozilla.org/en/The_DOM_and_JavaScript "You would then use the DOM "alert()" method to display the string in a dialog to the user." – Skilldrick Oct 14 '10 at 12:50
  • @Rahul - see my link: "Other known implementations of the DOM include Perl, Java, ActiveX, Python, and probably others. This is of course only possible thanks to the language-neutrality of the DOM." – Skilldrick Oct 14 '10 at 12:53
  • @Rahul - you won't be able to safely use Python as a browser scripting language unless all the browsers you're supporting allow Python as a scripting language (which is basically none). – Skilldrick Oct 14 '10 at 12:55
  • The DOM is defined by W3C. There are 11 DOM standards. You can use my w3viewer.com to browse them. The alert method is not defined in any of them, so it is not a DOM method. There are literally hundreds of browser objects that are not defined by DOM standards and those objects are not considered part of the DOM. Some people don't understand this distinction, so they think that all browser objects are DOM objects. I assume that this mozilla article is somewhat old. – Šime Vidas Oct 14 '10 at 12:57
  • @Šime I think this depends on how strict your definition of the DOM is. If you're going to go by the standards then fine. But if you think of the DOM as the API by which JavaScript communicates with the browser, then my definition is valid. I agree that this definition is much looser and non-standard, but I've found it to be the most widely accepted definition. – Skilldrick Oct 14 '10 at 13:08
  • so can i manually integrate the DOM API and javascript ? well i know sounds ridiculous cuz thats wat a browser does , but i intend to understand browser more profoundly . – Rahul Oct 14 '10 at 13:11
  • @Skilldrick So do you consider all browser objects to be part of the DOM? What about Web Workers, Web storage, the File object, the XMLHttpRequest object, Indexed database? There are lots of APIs that are implemented in the browsers. It would be wrong to refer to them as part of the DOM. So, the line has to be drawn somewhere, and I believe that the best place to draw the line is: If the object/interface is defined by a DOM standard, then it is considered part of the DOM. – Šime Vidas Oct 14 '10 at 13:14
  • @Šime - OK, fair enough, that makes sense. – Skilldrick Oct 14 '10 at 16:38