dongyong8491 2010-09-04 09:37
浏览 835
已采纳

使用AJAX时为什么浏览器的地址栏不变?

I am using AJAX/JSON combination in Zend Framework. Whenever I click any link in my application, AJAX request is called and content is loaded successfully in DIVs But the address bar is unchanged. How to change address bar according to current active action.

Here is My Working Code:


When I use http://practice.dev/ my index.phtml file is loaded.

<a href='http://practice.dev/index/one' class='ajax'>One</a>
<a href='http://practice.dev/index/two' class='ajax'>Two</a>

<div id="content">content comes here</div>

one.phtml:

$jsonArray = array( 'content' => 'One' );
echo Zend_Json::encode( $jsonArray );

two.phtml:

$jsonArray = array( 'content' => 'Two' );
echo Zend_Json::encode( $jsonArray );

JS Code

jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event) {
        event.preventDefault();
        jQuery.getJSON(this.href, function(snippets) {
            for(var id in snippets) {
                jQuery('#' + id).html(snippets[id]);
            }
        });
    });
});

When I click link one then string 'One' is loaded in content DIV but address bar is still http://practice.dev/. It should be http://practice.dev/index/one

When I click link two then string 'Two' is loaded in content DIV but address bar is still http://practice.dev/. It should be http://practice.dev/index/two

How is it possible ?

Thanks

  • 写回答

2条回答 默认 最新

  • drd99007 2010-09-04 09:41
    关注

    Remove event.preventDefault();, this is stopping the default behavior for the link occurring. The default behavior being - going to the URL in the href attribute.

    Edit based on more info from OP

    You need to make your javascript respond to the hash tag.

    Instead of having a URL schema just like this http://practice.dev/index/one it needs to have a hash tag version too, that javascript can handle like this http://practice.dev/index#one. This means that if someone visits the page http://practice.dev/index#two your javascript can know that it needs to go and get the second page, even though Zend will have got the first page already.

    In your javascript you can get the hash tag using

    var hash = location.hash;
    

    And set it using

    location.hash = "two";
    

    There is more about this here

    So leave your links the same like this http://practice.dev/index/one but append a hash tag to the URL when the user clicks a link, you CAN add hash tags the address bar. Change your javascript to load a certain page number if there is a hash tag present when the page loads.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧