Book: "Pro JavaScript techniques proficient JavaScript" overload browser default behavior __java

Source: Internet
Author: User

The default behavior of the browser boils down to the fact that the browser automatically executes all of these behaviors without explicit instructions. The following are examples of different types of default behavior for different events:

1. Click on the URL of the <a> element to redirect to its href attribute

2. Using the keyboard and pressing Ctrl+s, the browser will save the HTML file for the Web site

3. Submitting html<form> will submit data to the specified URL and redirect the browser to the address

4. Mouse move to a have Alt or title (depending on browser) will appear toolbar, hint description

Even if the event is prevented from bubbling or the event is not bound at all, the browser will perform all of these behaviors. This can cause major problems in your script. If you want to submit a form in a different kind of presentation, or want to make a difference in the performance of the <a> element, rather than the original intent? Because canceling event bubbling does not prevent the default behavior, you need some specific code to handle it directly. As with the bubble event cancellation, there are two ways to prevent the default behavior from occurring: I E specific way and the way of the consortium. Two ways are shown in Listing 6-8. This function takes an event object parameter that passes in an event handler function, and should be used at the end of the time processing function, for example: return Stopdefault (e); ------because the processing function also needs to return False (false itself is returned from Stopdefault)

Code Listing 6-8 Common functions that prevent default browser behavior from occurring

function Stopdefault (e) {

Prevent default browser behavior (the Web-consortium)

if (E&&e.preventdefault)

E.preventdefault ();

Shortcuts to blocking browser behavior in IE

Else

Window.event.returnvalue=false;

return false;

}

Using the Stopevent function, you can block any default behavior of the browser. This allows you to write some clever interactions for your users, such as the example shown in Listing 6-9. The code makes all the links loaded in a built-in <iframe> Instead of reloading the entire page. This allows the user to stay on one page, so it is possible to gain a better interaction experience in some situation

Code Listing 6-9 uses Stopdefault () to Overload browser functionality

Suppose an IFrame already exists in the page, and its ID is ' iframe '

var Iframe=document.getelementbyid ("iframe");

Locate all <a> elements on the page

var a=document.getelementsbytagname ("a");

for (Var i=0;i<a.length;i++) {

To <a> bind fixed-point hit handler function

A[i].onclick=function (e) {

Set the address of an IFRAME

Iframe.src=this.href;

Prevents the browser from accessing the Web site that the <a> points to (this is a default behavior)

Return Stopdefault (e);

};

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.