Function to Navigate To Named Anchor

Below is  a helper function to insert javascript  to Navigate To Named Anchor

Page should have anchor with the passed name

<a name="anchorName"></a>

   

    // from http://www.webdeveloper.com/forum/showthread.php?threadid=112603

    ///<summary>///

    ///</summary>

    ///<param name="page"></param>

    ///<param name="anchorName"></param>

    ///<remarks>Page should have anchor with the passed name <a name="anchorName"></a>

    /// If there are more than one call on the page, first one will take preference

    ///</remarks>

    public static void NavigateToNamedAnchor(Page page, string anchorName)

    {

        String script = String.Format("window.location.hash = '{0}';", anchorName);

        //NOTE: if there are more than one call on the page, first one will take preference

        //If we want that last will take preference, change key from MethodBase.GetCurrentMethod().Name to anchorName

        page.ClientScript.RegisterStartupScript(TypeForClientScript(), MethodBase.GetCurrentMethod().Name, script, true);

        //return script;

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Wednesday, May 14, 2008 2:28 PM
Print

Comments on this entry:

# re: Function to Navigate To Named Anchor

Left by web development company at 8/18/2009 2:29 AM
Gravatar
Nice post,

"if there are more than one call on the page, first one will take preference"why should it be like that?

Anyway, thanks for the post

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910