JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides)

Category: Programming
Author: David Flanagan
4.5
All Stack Overflow 8
This Year Stack Overflow 1
This Month Stack Overflow 4

Comments

by anonymous   2019-07-21

Due to the lack of detail (and a slight smell of homework) I'm only going to give very general advice.

You need to do something after a time interval. jQuery doesn't have that built in, but it's simple enough to do without it: https://developer.mozilla.org/en/DOM/window.setTimeout

It sounds like you need to update text, jQuery can do that: http://api.jquery.com/text/

It is imperative to lean Javascript even while using jQuery. A good book can help there, something along the lines of this one. http://www.amazon.com/JavaScript-Definitive-Guide-Activate-Guides/dp/0596805527/ref=pd_sim_b_2

Strangely enough, the two answers that were posted while I was writing these have the exact same code, down to formatting and naming. And as such, they both contain a mistake.

by anonymous   2019-07-21

First, we could try standardizing your control.

<input type="submit" name="Submit" value="Submit" /> 

http://www.w3.org/TR/html401/interact/forms.html#h-17.4


Perhaps some clarification about buttons could help, too.

http://www.w3.org/TR/html401/interact/forms.html#h-17.5


"When I changed type to submit it worked but I do not want it to send me straight to the function where it inserts data. How do I stop it from submitting."

I'm a little rusty with my JavaScript, but I believe you can use the onsubmit attribute to do something, or you can register some functions using the onload="" attribute of the <form> tag or, more commonly, the <body> tag. Find a good JavaScript book with a section on forms.

http://www.amazon.com/JavaScript-Definitive-Guide-Activate-Guides/dp/0596805527/ref=sr_1_1?s=books&ie=UTF8&qid=1401399613&sr=1-1&keywords=javascript+the+definitive+guide

by anonymous   2019-07-21

I think scope chain is the thing you are looking here:

From http://www.amazon.com/JavaScript-Definitive-Guide-Activate-Guides/dp/0596805527/ref=sr_1_2?ie=UTF8&qid=1362066219&sr=8-2&keywords=javascript (3.10.3)

Every time a function is invoked, it creates a new object to store its local variables, and adds that new object to the stored scope chain to create a new, longer, chain that represents the scope for that function invocation.

For your question yes it will again go through the whole execution context, otherwise how can you return another function from bar() like

function bar() {
      var y = 2;
      alert(x + y);

return function() {alert('hello');}
    }
by anonymous   2019-07-21

From Javascript: The Definitive Guide, it clarifies things. It notes that HTMLElement objects of a HTML doc define JS properties that correspond to all standard HTML attributes.

So you only need to use setAttribute for non-standard attributes.

Example:

node.className = 'test'; // works
node.frameborder = '0'; // doesn't work - non standard attribute
node.setAttribute('frameborder', '0'); // works
by anonymous   2019-01-13

Here are my findings:

JavaScript: The Definitive Guide, written by David Flanagan provides a very concise explanation:

JavaScript was created at Netscape in the early days of the Web, and technically, "JavaScript" is a trademark licensed from Sun Microsystems (now Oracle) used to describe Netscape's (now Mozilla's) implementation of the language. Netscape submitted the language for standardization to ECMA and because of trademark issues, the standardized version of the language was stuck with the awkward name "ECMAScript". For the same trademark reasons, Microsoft's version of the language is formally known as "JScript". In practice, just about everyone calls the language JavaScript.

A blog post by Microsoft seems to agree with what Flanagan explains by saying..

ECMAScript is the official name for the JavaScript language we all know and love.

.. which makes me think all occurrences of JavaScript in this reference post (by Microsoft again) must be replaced by ECMASCript. They actually seem to be careful with using ECMAScript only in this, more recent and more technical documentation page.

w3schools.com seems to agree with the definitions above:

JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997. ECMA-262 is the official name of the standard. ECMAScript is the official name of the language.

The key here is: the official name of the language.

If you check Mozilla 's JavaScript version pages, you will encounter the following statement:

Deprecated. The explicit versioning and opt-in of language features was Mozilla-specific and are in process of being removed. Firefox 4 was the last version which referred to a JavaScript version (1.8.5). With new ECMA standards, JavaScript language features are now often mentioned with their initial definition in ECMA-262 Editions such as ECMAScript 2015.

and when you see the recent release notes, you will always see reference to ECMAScript standards, such as:

  • The ES2015 Symbol.toStringTag property has been implemented (bug 1114580).

  • The ES2015 TypedArray.prototype.toString() and TypedArray.prototype.toLocaleString() methods have been implemented (bug 1121938).

Mozilla Web Docs also has a page that explains the difference between ECMAScript and JavaScript:

However, the umbrella term "JavaScript" as understood in a web browser context contains several very different elements. One of them is the core language (ECMAScript), another is the collection of the Web APIs, including the DOM (Document Object Model).

Conclusion

To my understanding, people use the word JavaScript somewhat liberally to refer to the core ECMAScript specification.

I would say, all the modern JavaScript implementations (or JavaScript Engines) are in fact ECMAScript implementations. Check the definition of the V8 Engine from Google, for example:

V8 is Google’s open source high-performance JavaScript engine, written in C++ and used in Google Chrome, the open source browser from Google, and in Node.js, among others. It implements ECMAScript as specified in ECMA-262.

They seem to use the word JavaScript and ECMAScript interchangeably, and I would say it is actually an ECMAScript engine?

So most JavaScript Engines are actually implementing the ECMAScript standard, but instead of calling them ECMAScript engines, they call themselves JavaScript Engines. This answer also supports the way I see the situation.

by tequila_shot   2018-11-10
I keep ~10 books at my desk. 9 of them are related to Javascript / Python / Probability etc [1]., There is one book though, that I really love to see everyday. Arabian Nights. That was the first book that was gifted to me when I was 11. I always had it with me. It reminds me of my childhood when things get too stressed and I read excerpts out of this book.

[1] https://www.amazon.com/JavaScript-Definitive-Guide-Activate-... [2] https://www.amazon.com/Thinking-Fast-Slow-Daniel-Kahneman/dp... [3]https://www.amazon.com/Introduction-Probability-Models-Tenth... [4] https://www.amazon.com/Hackers-Black-Book-Important-Informat...

by anonymous   2017-08-20

Briefly, with more detail below,

  • window is the execution context and global object for that context's JS
  • document contains the HTML
  • screen describes the physical display's full screen

See W3C and Mozilla references for details about these objects. The most basic relationship among the three is that each browser tab has its own window, and a window has window.document and window.screen properties. The browser tab's window is the global context, so document and screen refer to window.document and window.screen. More details about the three objects are below, following Flanagan's JavaScript: Definitive Guide.

window

Each browser tab has its own top-level window object. Each <iframe> (and deprecated <frame>) element has its own window object too, nested within a parent window. Each of these windows gets its own separate global object. window.window always refers to window, but window.parent and window.top might refer to enclosing windows, giving access to other execution contexts. In addition to document and screen described below, window properties include

  • setTimeout() and setInterval() binding event handlers to a timer
  • location giving the current URL
  • history with methods back() and forward() giving the tab's mutable history
  • navigator describing the browser software

document

Each window object has a document object to be rendered. These objects get confused in part because HTML elements are added to the global object when assigned a unique id. E.g., in the HTML snippet

<body>
  <p id="holyCow"> This is the first paragraph.</p>
</body>

the paragraph element can be referenced by any of the following:

  • window.holyCow or window["holyCow"]
  • document.getElementById("holyCow")
  • document.body.firstChild
  • document.body.children[0]

screen

The window object also has a screen object with properties describing the physical display:

  • screen properties width and height are the full screen

  • screen properties availWidth and availHeight omit the toolbar

The portion of a screen displaying the rendered document is the viewport in JavaScript, which is potentially confusing because we call an application's portion of the screen a window when talking about interactions with the operating system. The getBoundingClientRect() method of any document element will return an object with top, left, bottom, and right properties describing the location of the element in the viewport.

by anonymous   2017-08-20

Understanding pure JavaScript first would be your first objective and learning how objects are defined there. I recommend the JavaScript: the definitive guide By David Flanagan.

Once understanding the JS model you can start looking under the hood of jQuery and understand what is being done. jQuery just takes advantage of the JavaScript language making an easy to use framework: http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper

by anonymous   2017-08-20

where to begin... first of all it is considered good practice to put your javascript at the bottom of the document right before the closing body tag. see the following link for an explanation as to why that is:

http://developer.yahoo.com/performance/rules.html#js_bottom

when you include javascript files at the bottom of the document you will be forced to enclose your code into functions and bind those functions to different events. javascript is very much an event driven language.

this will encourage you to write better code that will be reusable throughout your project. I wont go into a big long lecture on writing good code but i recommend picking up a good book to get you started. like Javascript The Definitive Guide.

as to the code you currently have, here's how I would do it:

<!DOCTYPE html>
<html>
    <head>
    </head>

    <body>
      <select id="dd_city"></select>
      <script>
          var initPage = function() {
              var cities = Array("London", "New York");
              var selectBox = document.getElementById("dd_city");

              for( var i = 0; i < cities.length; i++)
              {
                  var option = document.createElement("option");

                  option.setAttribute("value", i);
                  option.innerHTML = cities[i];
                  selectBox.appendChild(option);
              }
          };

          window.onload = initPage();
      </script>
    </body>
</html>

see it in action at my jsfiddle.

by anonymous   2017-08-20

1) this.foo = bar

The first kind you mention makes your variables properties of a (constructor) function. In JavaScript, functions are objects. Thus, using this makes a variable a property of an object (your constructor function, in this case).

2) var x = 7

Let me start off saying that anytime you do not use var in front of a variable (save for the this stuff above), that variable will become a member of the global context object (window, for most user-agents).

Thought leaders in JavaScript and programming see global variables as a sin, or code smell. Avoid global variables as much as possible.

The issue with the first method (this) is that prior to ECMAScript 5, there was no good way to enforce privacy on object properties (as you might find in the classical C++ based languages like Java, PHP, etc). ECMAScript 5 and above allow you to assign attributes to properties for things like writability, enumerability, etc...

However, as it pertains to var, well, var always makes its variables private to the function object. You cannot access a var variable from external, client code like this

dog.age = 5 //Trying to assign the number five to a var variable.

You can make a function that has access (known as an accessor), though. In effect, when you see var, think private to this object's scope. That's the gist of it. Closure (from enclosure) is an important subject in JavaScript. When you get into using var, inevitably you start to learn about closure.

3) No key word.

Finally, using naked variables attaches all of them to the global object (window, for most user-agents). This tends to lead to confusion and debugging issues. You may inadvertently stage a collision or conflict where a value is being changed across many instances of your constructor function. It's just not the way to go.

In summary, a fantastic book on the subject of JavaScript variables is The Principles of Object Oriented JavaScript. Mr. Zakas explains things much better than Crockford and Flannagan. I highly recommend it. Know your goal. Know your problem. Avoid global variables. Learn about JavaScript's object mode and scope. I recommend JavaScript: The Definitive Guide when it comes to learning about scope and context in JavaScript.