JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides)
All
Stack Overflow 8
This Year
Stack Overflow 1
This Month
Stack Overflow 4
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.
First, we could try standardizing your control.
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 theonload=""
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
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
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:
Here are my findings:
JavaScript: The Definitive Guide, written by David Flanagan provides a very concise explanation:
A blog post by Microsoft seems to agree with what Flanagan explains by saying..
.. which makes me think all occurrences of
JavaScript
in this reference post (by Microsoft again) must be replaced byECMASCript
. They actually seem to be careful with usingECMAScript
only in this, more recent and more technical documentation page.w3schools.com seems to agree with the definitions above:
The key here is: the official name of the language.
If you check Mozilla 's JavaScript version pages, you will encounter the following statement:
and when you see the recent release notes, you will always see reference to ECMAScript standards, such as:
Mozilla Web Docs also has a page that explains the difference between ECMAScript and JavaScript:
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:
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.
[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...
Briefly, with more detail below,
window
is the execution context and global object for that context's JSdocument
contains the HTMLscreen
describes the physical display's full screenSee 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 awindow
haswindow.document
andwindow.screen
properties. The browser tab'swindow
is the global context, sodocument
andscreen
refer towindow.document
andwindow.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 ownwindow
object too, nested within a parent window. Each of these windows gets its own separate global object.window.window
always refers towindow
, butwindow.parent
andwindow.top
might refer to enclosing windows, giving access to other execution contexts. In addition todocument
andscreen
described below,window
properties includesetTimeout()
andsetInterval()
binding event handlers to a timerlocation
giving the current URLhistory
with methodsback()
andforward()
giving the tab's mutable historynavigator
describing the browser softwaredocument
Each
window
object has adocument
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 snippetthe paragraph element can be referenced by any of the following:
window.holyCow
orwindow["holyCow"]
document.getElementById("holyCow")
document.body.firstChild
document.body.children[0]
screen
The
window
object also has ascreen
object with properties describing the physical display:screen properties
width
andheight
are the full screenscreen properties
availWidth
andavailHeight
omit the toolbarThe 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 anydocument
element will return an object withtop
,left
,bottom
, andright
properties describing the location of the element in the viewport.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
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:
see it in action at my jsfiddle.
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 thethis
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 assignattributes
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 thisYou 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 usingvar
, 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.