【问题标题】:Using ajax to append variable to address bar使用ajax将变量附加到地址栏
【发布时间】:2014-02-25 05:14:08
【问题描述】:

有没有办法在不重新加载页面的情况下将var url 变量附加到地址栏。这是我第一次使用ajax。

编辑:事件目标节点是什么 编辑2:明白了,比我想象的要简单得多..

history.pushState(null, null, url);


        $('a[href^="/"]').unbind('click').click(function(e){
            var url = $(this).attr('href');
            e.preventDefault();
            $.ajax({
                url: url,
                success: function( result ){
                    response = $('<div id="temp-ajax-response-holder" style="display: none;"></div>');

                    response.html(result);
                    if (response.find('#ajax').first().find('.header').length > 0 ) $('body').removeClass("home archive").addClass('single');
                    else if(response.find('#ajax').first().find('.archive_title').length > 0 ) $('body').removeClass("home single").addClass('archive')
                    else $('body').removeClass("single archive").addClass('home');
                    if (response.find('#ajax').first().find('.header').length > 0 )
                        $('body').attr('class', 'single single-post');
                    $('body #ajax').html(response.find('#ajax').first().html());
                    bindHTML();
                    response.html('');

                }
            });
    });

【问题讨论】:

标签: jquery ajax history


【解决方案1】:

我首先想到的是 History API。如果您的浏览器支持它,您可以更改地址栏中的位置,甚至通过浏览器历史记录使其可用。例如,这允许使用后退按钮。

history.pushState(null, null, yourNewUrl);

如需进一步介绍,我建议阅读http://diveintohtml5.info/history.html

【讨论】:

  • 喜欢这个 history.pushState(null, null, url);
【解决方案2】:

是的,有,仅适用于支持 HTML 5 的浏览器。

有一个推送状态。 检查此链接

http://diveintohtml5.info/history.html

演示是

http://html5demos.com/history

【讨论】:

    猜你喜欢
    • 2015-11-27
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多