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

javascript - Process anchor (hash) links with jQuery and Ajax - Stack Overflow

programmeradmin0浏览0评论

Suppose I want to navigate to the following link:


The content of is loaded with jQuery ajax, so I cannot navigate to #linktosection until the ajax content has been loaded. Once it is loaded I need to navigate (maybe simulate a click) to #linktosection

This is my jQuery ajax call:

$('.changecontext-button').click(function()
{
    $.ajax({                                                           
        type: "POST",                                                  
        url: $(this).attr('href'),
        success: function(data) {
            diffSection.html(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            diffSection.html(xhr.responseText);
        }
    });
});

Do you know how to do this with jQuery?

An alternative could be parsing the href link and separate it into a base url and an anchor url. The, on success I could use a jQuery selector to get the anchor link and simulate a .click().

Is there any other better alternative, using jQuery or JavaScript?

Thanks in advanced.

Suppose I want to navigate to the following link:

http://www.mysite./feature#linktosection

The content of http://www.mysite./feature is loaded with jQuery ajax, so I cannot navigate to #linktosection until the ajax content has been loaded. Once it is loaded I need to navigate (maybe simulate a click) to #linktosection

This is my jQuery ajax call:

$('.changecontext-button').click(function()
{
    $.ajax({                                                           
        type: "POST",                                                  
        url: $(this).attr('href'),
        success: function(data) {
            diffSection.html(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            diffSection.html(xhr.responseText);
        }
    });
});

Do you know how to do this with jQuery?

An alternative could be parsing the href link and separate it into a base url and an anchor url. The, on success I could use a jQuery selector to get the anchor link and simulate a .click().

Is there any other better alternative, using jQuery or JavaScript?

Thanks in advanced.

Share Improve this question edited Dec 10, 2010 at 16:56 Daniel Peñalba asked Dec 10, 2010 at 15:07 Daniel PeñalbaDaniel Peñalba 31.9k34 gold badges142 silver badges226 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Finally, I implemented it as follows:

$('.changecontext-button').click(function()
{
    var targetUrl = $(this).attr('href');
    $.ajax({                                                           
        type: "POST",                                                  
        url: targetUrl,
        success: function(data) {
            diffSection.html(data);
            var anchor = getAnchor(targetUrl);
            if (anchor)
            {
                $(anchor).click();
            }
        },
        error: function(xhr, textStatus, errorThrown) {
                diffSection.html(xhr.responseText);
        }
    });
});

...

function getAnchor(url)
{
    var index = url.lastIndexOf('#');
    if (index != -1)
        return url.substring(index);
}

After your content has loaded:

$('a[name=' + window.location.hash + ']').get(0).scrollIntoView();

This is, what works for me, when called after content is loaded:

window.location.hash = window.location.hash
发布评论

评论列表(0)

  1. 暂无评论