BUG: Redirection in Anchor Tag Using Location.href Fails

ID: Q185376


The information in this article applies to:


SYMPTOMS

Placing a call to location.href in an anchor tag does not cause navigation to be redirected to that HREF when the user clicks on the link.


CAUSE

This behavior is by design.


RESOLUTION

The workaround is to use the "javascript:" syntax for the HREF attribute of the anchor tag. Please see the MORE INFORMATION section for sample code that demonstrates this workaround.


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Insert the following HTML into a Web page:
    
       <HTML>
       <HEAD>
          <TITLE>Redirecting within an anchor tag</TITLE>
    
          <SCRIPT>
    
             function GoSomewhere()
             {
                location.href = "<LINK TYPE="GENERIC"    VALUE="http://www.microsoft.com";">http://www.microsoft.com";</LINK>
          }
          </SCRIPT>
       </HEAD>
       <BODY>
       <A NAME=SomeLink HREF=#book onclick=GoSomewhere()>Click Here&lt;/A
       </BODY>
       </HTML> 


  2. Click on the link at the top of the page. The result is that nothing happens.


The following HTML demonstrates two ways to work around this problem using the "javascript:" syntax. Using the workaround demonstrated in Link 2, you can determine the location to which you want to navigate.

Sample Code


   <HTML>
   <HEAD>

   <TITLE>Redirecting within an anchor tag</TITLE>

   <SCRIPT>
      function GoSomewhere()
      {
         location.href = "<LINK TYPE="GENERIC"             VALUE="http://www.microsoft.com";">http://www.microsoft.com";</LINK>
      }
   </SCRIPT>

   </HEAD>
   <BODY>

   <A NAME=Link1
      HREF="javascript:location.href='<LINK TYPE="GENERIC"       VALUE="http://www.microsoft.com'">">http://www.microsoft.com'"></LINK>
      Link 1</A>
      <BR>
   <A NAME=Link2 HREF="javascript:GoSomewhere()">Link 2</A>

   </BODY>
   </HTML> 


REFERENCES

For more information, please see the MSDN Web Workshop:

http://msdn.microsoft.com/workshop/default.asp

Additional query words: kbHTMLObj kbScript


Keywords          : kbIE400 kbIE401 kbDSupport 
Version           : WINDOWS:4.0,4.01
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: July 22, 1999