Href and onclick of

Source: Internet
Author: User

Http://gocom.primeton.com/blog21307_27051.htm

 

I used to write <A> href and onclick at will, but I have encountered several issues before I began to pay attention to this issue:

First, extract a document:

In JavaScript, void is an operator that specifies to calculate an expression but does not return a value.

The format of the void operator is as follows:
1. javascript: void (expression)
2. javascript: void expression

Expression is a standard JavaScript expression to be calculated. Parentheses outside the expression are optional, but it is a good habit to write them. (Implement version navigator 3.0)

You use the void operator to specify the hyperlink. The expression is calculated, but no content is loaded in the current document.

The following code creates a hyperlink that will not happen to users in the future. When a user links, void (0) is calculated as 0, but JavaScript has no effect.

<A href = "javascript: void (0)"> nothing happens here. </a>

The following code creates a hyperlink. A form is submitted when a user opens a ticket.

<A href = "javascript: void (document. Form. Submit ()">
Submit a form here </a>

The following code executes the subgo () function,

<A href = "javascript: void (0)" onclick = "subgo ()"> click me </a>

In this case, javascript: void (0) does not actually function. It is just a dead link and the executed function is subgo ().

<A href = "#" onclick = "subgo ()"> click me </a> and <a href = "javascript: void (0)" onclick = "subgo () "> click me </a>.

In fact, # contains a location information. The default anchor is # top, that is, the top of the Web page. javascript: void (0) only indicates a dead link, without any information. Therefore, it is best to use void (0) when calling the script)

Href generally points to a URL address and can also call JavaScript, such as href = "javascript: XXX ();". It is recommended in this document to write: <a href = "javascript: void (0) "onclick =" XXX (); "> XX </a>, but this method sometimes produces strange problems in complex environments. Try not to use JavaScript: as the href attribute of A, this will not only trigger the window. the onbeforeunload event stops playing GIF animated images in IE.

We know that the onclick event of the link is executed first, followed by the action under the href attribute (page Jump, or Javascript pseudo link). If you do not want to execute the action under the href attribute, onclick must return false. Generally, onclick = "XXX (); Return false ;".

The JS source code of tabpane. Because onclick does not return false, when tabpane is disabled in ifrmae, href is executed, and the page is displayed incorrectly. The solution is to copy the following code to the JSP using the tab.

Tabpane. Prototype. appendtitle = function (tabpage ){
VaR TD = $ create ("TD ");
VaR strhtml = "<Table class =" "+ tab_style_table +" "border =" 0 "cellspacing =" 0 "cellpadding =" 0 "> <tr> ";
Strhtml + = "<TD> <Div class =" "+ tab_style_left +" "> </div> </TD> ";
Strhtml + = "<TD nowarp class =" "+ tab_style_mid +" "> ";
Strhtml + = "<Div style =" white-space: nowrap; page-break-before: Always; page-break-after: Always; "> ";
Strhtml + = tabpage. gettitle ();
Strhtml + = "</div> </TD> ";
If (tabpage. showclosebutton ){
Strhtml + = "<TD nowarp class =" "+ tab_style_mid +" "> ";
Strhtml + = "<a class =" close "href =" # "onclick =" $ O (\ "" + this. ID + "\"). gettab (\ "" + tabpage. ID + "\"). close ();Return false;"> ";
If (isie ){
Strhtml + = "<div> </a> ";
} Else {
Strhtml + = "<Div class =" close "> </div> </a> ";
}
Strhtml + = "</TD> ";
}
If (tabpage. allowreload ){
Strhtml + = "<TD nowarp class =" "+ tab_style_mid +" "> ";
Strhtml + = "<a class =" reload "href =" # "onclick =" $ O (\ "" + this. ID + "\"). gettab (\ "" + tabpage. ID + "\"). reload ();Return false;"> ";
If (isie ){
Strhtml + = "<div> </a> ";
} Else {
Strhtml + = "<Div class =" reload "> </div> </a> ";
}
Strhtml + = "</TD> ";
}
Strhtml + = "<TD> <Div class =" "+ tab_style_right +" "> </div> ";
Strhtml + = "</TD> </tr> </table> ";
TD. classname = tab_style_nomal + this. stylesuffix;
TD. innerhtml = strhtml;
Tabpage. _ titletd = TD;
Tabpage. settitletd ();
Tabpage. initstatus ();
This. _ titletr. insertbefore (TD, this. _ titletd );
Tabpage. titlewidth = TD. offsetwidth;
}

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.