Tuesday, June 30, 2015
What is the main difference between window.onload and onDocumentReady?
Both functions are used to perform tasks when the page is loaded in the browser but they have important differences. Most importantly, "window.onload" will execute code when browser has loaded the DOM tree and all other resources like images, objects, etc, while onDocumentReady allows you to execute code when only the DOM tree has been built, without waiting for images to load. Thus, you can start scripting agains the DOM much quicker with onDocumentReady. Another important difference is that window.onload is not cross-browser compatible while using something like jQuery's document.ready() will work nicely on all browsers.
Monday, June 29, 2015
decodeURI(), encodeURI()
Many characters cannot be
sent in a URL, but must be converted to their hex encoding. These functions are
used to convert an entire URI (a superset of URL) to and from a format that can
be sent via a URI.
<script
type="text/javascript">
var uri =
"http://www.google.com/search?q=sonofusion Taleyarkhan"
document.write("Original
uri: "+uri);
document.write("<br
/>encoded: "+encodeURI(uri));
</script>
What's Prototypes for JavaScript?
Objects have
"prototypes" from which they may inherit fields and functions.
<script
type="text/javascript">
function movieToString() {
return("title:
"+this.title+" director: "+this.director);
}
function movie(title,
director) {
this.title = title;
this.director = director ||
"unknown"; //if null assign to "unknown"
this.toString =
movieToString; //assign function to this method pointer
}
movie.prototype.isComedy =
false; //add a field to the movie's prototype
var officeSpace = new
movie("OfficeSpace");
var narnia = new
movie("Narni","Andrew Adamson");
document.write(narnia.toString());
document.write("
Narnia a comedy?
"+narnia.isComedy);
officeSpace.isComedy = true;
//override the default just for this object
document.write("
Office Space a comedy?
"+officeSpace.isComedy);
</script>
eval()?
The eval() method is
incredibly powerful allowing you to execute snippets of code during execution.
<script
type="text/javascript">
var USA_Texas_Austin =
"521,289";
document.write("Population
is "+eval("USA_"+"Texas_"+"Austin"));
</script>
This produces
Population is 521,289
How to create a function using function constructor?
The following example
illustrates this
It creates a function called square
with argument x and returns x multiplied by itself.
var square = new Function
("x","return x*x");
What does break and continue statements do?
Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop
How to associate functions with objects using JavaScript?
Let's now create a custom
"toString()" method for our movie object. We can embed the function
directly in the object like this.
<script
type="text/javascript">
function movie(title,
director) {
this.title = title;
this.director = director;
this.toString = function
movieToString() {
return("title:
"+this.title+" director: "+this.director);
}
}
var narnia = new
movie("Narni","Andrew Adamson");
document.write(narnia.toString());
</script>
This produces
title: Narni director: Andrew
Adamson
Or we can use a previously
defined function and assign it to a variable. Note that the name of the
function is not followed by parenthesis, otherwise it would just execute the
function and stuff the returned value into the variable.
<script
type="text/javascript">
function movieToString() {
return("title:
"+this.title+" director: "+this.director);
}
function movie(title,
director) {
this.title = title;
this.director = director;
this.toString =
movieToString; //assign function to this method pointer
}
var aliens = new
movie("Aliens","Cameron");
document.write(aliens.toString());
</script>
This produces
title: Aliens director:
Cameron
Subscribe to:
Posts (Atom)
What is restrict option in directive?
The restrict option in angular directive, is used to specify how a directive will be invoked in your angular app i.e. as an attribute, class...
-
JavaScript is a general-purpose programming language designed to let programmers of all skill levels control the behavior of software objec...
-
The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with...