What Is Javascript?: Thanks For Downloading. Enjoy The Reading
What Is Javascript?: Thanks For Downloading. Enjoy The Reading
1.Donate money.
Please go through the link to donate
http://www.downloadmela.com/donate.html
What is JavaScript?
A1: JavaScript is a general-purpose programming language designed to let programmers of all skill
levels control the behavior of software objects. The language is used most widely today in Web
browsers whose software objects tend to represent a variety of HTML elements in a document and the
document itself. But the language can be--and is--used with other kinds of objects in other
environments. For example, Adobe Acrobat Forms uses JavaScript as its underlying scripting language
to glue together objects that are unique to the forms generated by Adobe Acrobat. Therefore, it is
important to distinguish JavaScript, the language, from the objects it can communicate with in any
particular environment. When used for Web documents, the scripts go directly inside the HTML
documents and are downloaded to the browser with the rest of the HTML tags and content.
How can JavaScript be used to improve the "look and feel" of a Web site? By the same token,
how can JavaScript be used to improve the user interface?
On their own, Web pages tend to be lifeless and flat unless you add animated images or more
bandwidth-intensive content such as Java applets or other content requiring plug-ins to operate
(ShockWave and Flash, for example).
Embedding JavaScript into an HTML page can bring the page to life in any number of ways. Perhaps
the most visible features built into pages recently with the help of JavaScript are the so-called image
rollovers: roll the cursor atop a graphic image and its appearance changes to a highlighted version as a
feedback mechanism to let you know precisely what you're about to click on. But there are less visible
yet more powerful enhancements to pages that JavaScript offers.
Interactive forms validation is an extremely useful application of JavaScript. While a user is entering
data into form fields, scripts can examine the validity of the data--did the user type any letters into a
phone number field?, for instance. Without scripting, the user has to submit the form and let a server
program (CGI) check the field entry and then report back to the user. This is usually done in a batch
mode (the entire form at once), and the extra transactions take a lot of time and server processing
power. Interactive validation scripts can check each form field immediately after the user has entered
the data, while the information is fresh in the mind.
Another helpful example is embedding small data collections into a document that scripts can look up
without having to do all the server programming for database access. For instance, a small company
could put its entire employee directory on a page that has its own search facility built into the script.
You can cram a lot of text data into scripts no larger than an average image file, so it's not like the user
has to wait forever for the data to be downloaded.
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
Now our array scrips has 4 elements inside it and we can print or access them by using their index
number. Note that index number starts from 0. To get the third element of the array we have to use the
index number 2 . Here is the way to get the third element of an array.
document.write(scripts[2]);
We also can create an array like this
var no_array = new Array(21, 22, 23, 24, 25);
How do you target a specific frame from a hyperlink?
Include the name of the frame in the target attribute of the hyperlink. <a href=”mypage.htm”
target=”myframe”>>My Page</a>
What is a fixed-width table and its advantages?
Fixed width tables are rendered by the browser based on the widths of the columns in the first row,
resulting in a faster display in case of large tables. Use the CSS style table-layout:fixed to specify a
fixed width table.
If the table is not specified to be of fixed width, the browser has to wait till all data is downloaded and
then infer the best width for each of the columns. This process can be very slow for large tables.
Example of using Regular Expressions for syntax checking in JavaScript
...
var re = new RegExp("^(&[A-Za-z_0-9]{1,}=[A-Za-z_0-9]{1,})*$");
var text = myWidget.value;
var OK = re.test(text);
if( ! OK ) {
alert("The extra parameters need some work.\r\n Should be something like: \"&a=1&c=4\"");
cookies.txt
c:\Program Files\Netscape\Users\username\cookies.txt
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
c:\Windows\Cookies\username@Website.txt
Another useful approach is to set the "type" to "button" and use the "onclick" event.
<script type="text/javascript">
function displayHero(button) {
alert("Your hero is \""+button.value+"\".");
}
</script>
What does the term sticky session mean in a web-farm scenario? Why would you use a sticky
session? What is the potential disadvantage of using a sticky session?
Sticky session refers to the feature of many commercial load balancing solutions for web-farms to route
the requests for a particular session to the same physical machine that serviced the first request for that
session. This is mainly used to ensure that a in-proc session is not lost as a result of requests for a
session being routed to different servers. Since requests for a user are always routed to the same
machine that first served the request for that session, sticky sessions can cause uneven load distribution
across servers.
You have an ASP.NET web application running on a web-farm that does not use sticky sessions - so
the requests for a session are not guaranteed to be served the same machine. Occasionally, the users get
error message Validation of viewstate MAC failed. What could be one reason that is causing this error?
The most common reason for this error is that the machinekey value in machine.config is different for
each server. As a result, viewstate encoded by one machine cannot be decoded by another. To rectify
this, edit the machine.config file on each server in the web-farm to have the same value for
machinekey.
To set all checkboxes to true using JavaScript?
//select all input tags
function SelectAll() {
var checkboxes = document.getElementsByTagName("input");
for(i=0;i<checkboxes.length;i++) {
if(checkboxes.item(i).attributes["type"].value == "checkbox") {
checkboxes.item(i).checked = true;
}
}
}
How to select an element by id and swapping an image ? ...
<script language="JavaScript" type="text/javascript" >
function setBeerIcon() {
...
<p id="firstP">firstP<p>
</body>
</html>
How to have an element invoke a javascript on selection, instead of going to a new URL: ?
<script type="text/javascript">
function pseudoHitMe() {
alert("Ouch!");
}
</script>
<a href="javascript:pseudoHitMe()">hit me</a>
How to have the status line update when the mouse goes over a link (The support of the status
line is sporadic)?
<a href="javascript.shtml"
onmouseover="window.status='Hi There!';return true"
onmouseout="window.status='';return true">Look at the Status bar</a>
Look at the Status bar as your cursor goes over the link.
function setValueFromCookie(widget) {
if( getCookieData(widget) != "") {
document.getElementById(widget).value = getCookieData(widget);
}
}
//if you name your cookies the widget ID, you can use the following helper function
function setCookie(widget) {
document.cookie = widget + "=" +
escape(document.getElementById(widget).value) + getExpirationString();
}
How to change style on an element?
Between CSS and javascript is a weird symmetry. CSS style rules are layed on top of the DOM. The
CSS property names like "font-weight" are transliterated into "myElement.style.fontWeight". The class
of an element can be swapped out. For example:
document.getElementById("myText").style.color = "green";
document.getElementById("myText").style.fontSize = "20";
-or-
document.getElementById("myText").className = "regular";
Or, interestingly enough you can just assign the event's name on the object directly with a reference to
the method you want to assign.
You can also use the W3C addEvventListener() method, but it does not work in IE yet:
Key Events
"onkeydown", "onkeypress", "onkeyup" events are supported both in ie and standards-based browsers.
<script type="text/javascript">
function setStatus(name,evt) {
evt = (evt) ? evt : ((event) ? event : null); /* ie or standard? */
var charCode = evt.charCode;
var status = document.getElementById("keyteststatus");
var text = name +": "+evt.keyCode;
status.innerHTML = text;
status.textContent = text;
}
</script>
<form action="">
<input type="text" name="keytest" size="1" value=""
onkeyup="setStatus('keyup',event)"
onkeydown="setStatus('keydown',event)"
/>
<p id="keyteststatus">status</p>
</form>
if ( x == y) {
myElement.style.visibility = 'visible';
} else {
myElement.style.visibility = 'hidden';
}
How to set the cursor to wait ?
In theory, we should cache the current state of the cursor and then put it back to its original state.
document.body.style.cursor = 'wait';
//do something interesting and time consuming
document.body.style.cursor = 'auto';
How to reload the current page ?
window.location.reload(true);
how to force a page to go to another page using JavaScript ?
<script language="JavaScript" type="text/javascript" ><!--
location.href="http://newhost/newpath/newfile.html"; //--></script>
This produces
<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("days[\"Monday\"]:"+days["Monday"]);
</script>
This produces
days["Monday"]:Monday
How to use "join()" to create a string from an array using JavaScript?
"join" concatenates the array elements with a specified seperator between them.
<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("days:"+days.join(","));
</script>
This produces
days:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
How to make a array as a stack using JavaScript?
The pop() and push() functions turn a harmless array into a stack
<script type="text/javascript">
var numbers = ["one", "two", "three", "four"];
numbers.push("five");
numbers.push("six");
document.write(numbers.pop());
document.write(numbers.pop());
document.write(numbers.pop());
</script>
This produces
sixfivefour
How to shift and unshift using JavaScript?
<script type="text/javascript">
var myMovie = new Object();
myMovie.title = "Aliens";
myMovie.director = "James Cameron";
document.write("movie: title is \""+myMovie.title+"\"");
<
This produces
movie: title is "Aliens"
To create an object you write a method with the name of your object and invoke the method with
"new".
<script type="text/javascript">
function movie(title, director) {
this.title = title;
this.director = director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>
This produces
aliens:[object Object]
You can also use an abbreviated format for creating fields using a ":" to separate the name of the field
from its value. This is equivalent to the above code using "this.".
<script type="text/javascript">
function movie(title, director) {
title : title;
director : director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>
This produces
<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
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
<script type="text/javascript">
var myvalue = "Sir Walter Scott";
document.write("Original myvalue: "+myvalue);
document.write("<br />escaped: "+escape(myvalue));
document.write("<br />uri part: \"&author="+escape(myvalue)+"\"");
</script>
If you use escape() for the whole URI... well bad things happen.
<script type="text/javascript">
var uri = "http://www.google.com/search?q=sonofusion Taleyarkhan"
document.write("Original uri: "+uri);
document.write("