Cause Analysis of window. Location. href ineffective
December 29, 2007 | classification: JS + Ajax | Tag: jslocation | popularity: 2,170 ℃
Today I wrote a jump JS and used window. Location. href. The Code is as follows:
Function showenquirylistbytype (typeid ){
VaR uurl = "/enquiry/LIST /? Type = enquery & calss = "+ typeid;
Alert (uurl );
Window. Location. href = uurl;
}
The above alert (uurl) is used for debugging after no jump. The page is reflected as the actual uurl value, but does not jump. The analysis logic is window. location. the href has a problem, so we tried common debugging methods, such:
Determine whether it is on the include page: Use
Window. Parent. Location. href = uurl; // This is not the case because
Window. href location. href document. href is invalid.
So I jumped out of the judgment function and encountered an internal error. I analyzed the event of the function on the page. It turned out to be like this:
<A href = "javascript: void (0)" onclick = "showenquirylistbytype ('000000')">
Detected problem: <a href = "javascript: void (0)", changed:
<A href = "javascript: showenquirylistbytype ('000000')">
So everything is normal!
Why is this problem? Let's take a look at javascript: void (0 ):
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. (Implement version navigator 3.0)
You can use the void operator to specify a hyperlink. The expression is calculated, but no content is loaded in the current document.
The following code creates a hyperlink. Nothing happens after you click it. When a user clicks a link, void (0) is calculated as 0, but it has no effect on JavaScript.
<A href = "javascript: void (0)"> click here. </a>
The following code creates a hyperlink. When you click it, the form is submitted.
<A href = "javascript: void (document. Form. Submit ()"> click here to submit a form </a>