最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET navigate to anchor in code behind - Stack Overflow

programmeradmin4浏览0评论

I have a simple Web Form with code like this:

//...
//tons of text
//...
<a name="message" />
//...
//tons of text
//...
<asp:Button ID="ButtonSend" 
                runat="server" 
                text="Send"
                onclick="ButtonSend_Click" />

After POST I want to navigate user to my anchor "message". I have following code for this:

protected void ButtonSend_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.location.hash='#message';",
                                        true);
}

This simple JavaScript is not working in Firefox 3.5.2 - url is changing in browser but page is not navigated to anchor. In IE 8 it works perfectly.

Why this JavaScript code is not working in Firefox? Am I missing something?

I have a simple Web Form with code like this:

//...
//tons of text
//...
<a name="message" />
//...
//tons of text
//...
<asp:Button ID="ButtonSend" 
                runat="server" 
                text="Send"
                onclick="ButtonSend_Click" />

After POST I want to navigate user to my anchor "message". I have following code for this:

protected void ButtonSend_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.location.hash='#message';",
                                        true);
}

This simple JavaScript is not working in Firefox 3.5.2 - url is changing in browser but page is not navigated to anchor. In IE 8 it works perfectly.

Why this JavaScript code is not working in Firefox? Am I missing something?

Share Improve this question edited Aug 20, 2009 at 17:29 GTD asked Aug 20, 2009 at 17:16 GTDGTD 3253 gold badges8 silver badges17 bronze badges 1
  • seems to be working for me ... using FF 3.0.13 // IE8 both browser scrolled down perfectly ... i do think it should be working but you could try Cleiton code if ever its not – NakedLuchador Commented Aug 20, 2009 at 17:45
Add a ment  | 

4 Answers 4

Reset to default 7

I've solved my problem. JavaScript code was called before my anchor even existed. That's why Firefox wasn't scroll page down.

My code looks now like tihs:

this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.onload = function() {window.location.hash='#message';}",
                                         true);

After page load I'm calling my simple JavaScript function.

The key to found solution was Cleiton answer. Firebug reports that getElementById was returning null reference. Then I looked at generated HTML like andrewWinn suggested - JavaScript was called before anchor existed. Made little googling and found solution.

Thanks for your answers!

Mendoza, you can use native scrollIntoView function.

To do what you want, just write:

this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "document.getElementById('id_of_the_element').scrollIntoView();",
                                        true);

I ran into this once. Have you taken a look at the actual HTML? If I remember, FireFox was being case sensative on the anchors. I don't know if that changed recently or not.

You could try using the jQuery LocalScroll plugin. Using this, you could scroll using:

$(function() {
    $.scrollTo("#message");
});

Or with your ASP.NET code:

protected void ButtonSend_Click(object sender, EventArgs e)
{
    this.ClientScript.RegisterStartupScript(this.GetType(),
                                            "navigate",
                                            "$(function() { $.scrollTo('#message'); });",
                                             true);
}

It's not an ideal solution, especially if you're not using jQuery anywhere in your project already, but it might solve your problem.

发布评论

评论列表(0)

  1. 暂无评论