The fifth chapter of JavaScript programming can be a good habit to develop

Source: Internet
Author: User

A new window pops up when a user clicks on a link
JavaScript uses the Window object's open () method to create a new browser window, which has three parameters:
window.open (Url,name,features)
URL: New window address, if omitted, a blank window pops up
Name: Names of new windows
Features: The properties of the new window.

Like what:

function Open_url (URL) {            window.open (URL, "New-window", "width:320,height:480");        }       

Use the "javascript:" pseudo-Protocol to invoke the above function. ("javascript:" Pseudo-protocol allows us to invoke JavaScript functions via a link)

<Body><ahref= "Javascript:open_url (' http://www.baidu.com ');">Baidu</a><Scripttype= "Text/javascript">functionopen_url (URL) {window.open (URL),"New-window","width:320,height:480");} </Script></Body>

Embed <a> tag as attribute with OnClick event handler function

<ahref="#"onclick= "Open_url (' http://www.baidu.com '); return false;">Baidu 2</a><Scripttype= "Text/javascript">functionopen_url (URL) {window.open (URL),"New-window","width:320,height:480");} </Script>

The above two methods do not have a fallback, if the user disables the browser's JAVASCRIPT1 function, such a link is useless.

Reserve retreat that is, although the browser does not support JS or the user to disable JS some of the features can not be used, but the most basic operation can still be completed successfully.

<!--the practice of reserving retreat -<ahref= "Http://www.baidu.com"onclick= "Open_url (' http://www.baidu.com '); return false">Baidu 3</a><!--a simplified version of the reserve retreat -<ahref= "Http://www.baidu.com"onclick= "Open_url (this.getattribute (' href '); return false">Baidu 4</a><!--The most streamlined version of the reserve retreat -<ahref= "Http://www.baidu.com"onclick= "Open_url (this.href); return false">Baidu 4</a><!--the This.href property provided by the DOM -

Now even if JavaScript is disabled, this link is also available.

Behavior and structure separation:

<ahref= "Http://www.baidu.com"class= "link">Baidu 5</a><Scripttype= "Text/javascript">functionopen_url (URL) {window.open (URL),"New-window","width:320,height:480");} Window.onload=preparelinks;functionpreparelinks () {varlinks=document.getElementsByTagName ('a');  for (varI=0; I<links.length; I++){        if(Links[i].getattribute ('class') == 'Link') {Links[i].onclick= function() {Open_url ( This. getattribute ('href')); return false; }        }    }}   </Script>

The fifth chapter of JavaScript programming can be a good habit to develop

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.