使用 jQuery Address 进行 AJAX 深度链接

发布于 2024-08-30 01:27:57 字数 1307 浏览 2 评论 0原文

我有一个有很多页面的网站:

例如:

主页:http://mywebsite.com/index.html

某些页面: http://mywebsite.com/categorie/somepage.html

我决定使用 AJAX 动态加载我的页面,而无需重新加载页面。所以我决定使用 jQuery Address 插件 ( http://www .asual.com/jquery/address/docs/ )以允许深度链接和后退导航:

<script type="text/javascript" src="uploads/scripts/jquery.address-1.2rc.min.js"></script>
<script type="text/javascript">
                $('a').address(function() {
                    return $(this).attr('href').replace(/^#/, '');
                });
</script>

现在,安装插件后,如果我继续 http://mywebsite.com/index .html (HOME) 并点击 SOME PAGE 链接,jquery 成功加载 http://mywebsite.com/categorie/somepage.html 无需重新加载页面,浏览器上的地址栏就会显示: http://mywebsite.com/index.html/#/categorie/somepage.html 太棒了!

但是,问题是:如果我复制此动态生成的 URL:http://mywebsite.com/index.html/#/categorie/somepage.html 进入网络浏览器地址栏,它将进入我的网站 index.html 页面,而不是“SOME PAGE”页面。此外,前进/后退按钮无法正常工作,它们仅替换 URL 栏中的地址,但内容保持不变。

我想我需要编写一些 JavaScript 规则将动态 URL 与正确的页面关联起来?

一些帮助将不胜感激。谢谢 :)

I have a website which has many pages:

For example:

HOME: http://mywebsite.com/index.html

SOME PAGE:
http://mywebsite.com/categorie/somepage.html

I decided to make my pages load dynamically with AJAX without reloading the page. So I decided to use jQuery Address plugin ( http://www.asual.com/jquery/address/docs/ ) in order to allow deeplinking and Back-Forward navigation:

<script type="text/javascript" src="uploads/scripts/jquery.address-1.2rc.min.js"></script>
<script type="text/javascript">
                $('a').address(function() {
                    return $(this).attr('href').replace(/^#/, '');
                });
</script>

Now, after installing the plugin, if I go on http://mywebsite.com/index.html (HOME) and click on SOME PAGE link, jquery successfully loads the http://mywebsite.com/categorie/somepage.html without reloading the page and the address bar on my browser displays:
http://mywebsite.com/index.html/#/categorie/somepage.html which is great!

However, the problem is: if I copy this dynamically generated URL: http://mywebsite.com/index.html/#/categorie/somepage.html
into a web browser address bar, it will take into my website index.html page and not to the "SOME PAGE" page. Also, The Forward/Back buttons don't work correctly, they only replace the address in the URL bar but the content stays the same.

I suppose that I need to write some JavaScript rule that associates the dynamic URL with the correct page?

Some help would be appreciated. Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-09-06 01:27:57

将 url 复制粘贴到地址栏可以正常工作
后退/下一步按钮也可以使用。

# 应该在那里让它工作

任何人都知道如何使 #! 以便它可以被谷歌索引吗?

#/ 是同一件事吗?

基本上这对我来说在 WordPress 上工作:

// ajax setup
$.ajaxSetup({cache:false, success: function() { 
// optional action here
}});  

// Event handlers
$.address.init(function(event) {
  $('#nav li a').address(function() {
  return $(this).attr('href').replace(location.pathname, '');
   });
 }).bind('externalChange', {}, function(event) {
    var post_id; // get page id
    var nav_id; // get nav class
    // for back button 
    switch (true) {
     case (event.value == undefined):
           post_id = 2; nav_id = 'home'; break;
     case (event.value == "/about"):
           post_id = 19; nav_id = 'about'; break;
     case (event.value == "/product"):
           post_id = 26; nav_id = 'product'; break;

    ...etc....

     default: post_id = 2; nav_id = 'home';
    }

    // content loader on back/next button
    $("#content-wrap").load("http://www.somesite.com/home/",{id:post_id}); // load content
 });   

 // content loader by #nav 
 $(document).on("click","#nav li a",function(e){                                         
   var post_id = $(this).attr("id"); // get page id   
   $("#content-wrap").load("http://www.somesite.com/home/",{id:post_id}); // load content  
   return false; // keep url, no refresh
 });

copy paste url to address bar is working
back/next button also working.

the # should be there to make it work

anyone know how to make #! so its indexable by google?

or #/ is the same thing?

basically this work for me on wordpress:

// ajax setup
$.ajaxSetup({cache:false, success: function() { 
// optional action here
}});  

// Event handlers
$.address.init(function(event) {
  $('#nav li a').address(function() {
  return $(this).attr('href').replace(location.pathname, '');
   });
 }).bind('externalChange', {}, function(event) {
    var post_id; // get page id
    var nav_id; // get nav class
    // for back button 
    switch (true) {
     case (event.value == undefined):
           post_id = 2; nav_id = 'home'; break;
     case (event.value == "/about"):
           post_id = 19; nav_id = 'about'; break;
     case (event.value == "/product"):
           post_id = 26; nav_id = 'product'; break;

    ...etc....

     default: post_id = 2; nav_id = 'home';
    }

    // content loader on back/next button
    $("#content-wrap").load("http://www.somesite.com/home/",{id:post_id}); // load content
 });   

 // content loader by #nav 
 $(document).on("click","#nav li a",function(e){                                         
   var post_id = $(this).attr("id"); // get page id   
   $("#content-wrap").load("http://www.somesite.com/home/",{id:post_id}); // load content  
   return false; // keep url, no refresh
 });

别忘他 2024-09-06 01:27:57

像这样的东西:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        $.address.value(path);
    }
});

更新:

如果您手动加载页面(例如在链接点击时)而不是使用地址事件处理程序(例如$.address.change(function () { .. })),然后将上面的 $.address.value(path); 替换为对 path 处页面的 Ajax 调用:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        // get page at path
    }
});

Something like this:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        $.address.value(path);
    }
});

Update:

If you're loading pages manually (e.g. on link click) instead of using an address event handler (e.g. $.address.change(function () { ... })), then replace the $.address.value(path); above with an Ajax call for the page at path:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        // get page at path
    }
});
落花浅忆 2024-09-06 01:27:57

您可以通过添加以下内容使其可被 Google 索引 (#!):
$.address.crawlable(true);
这是我发现对 jQuery Address 方法很有帮助的链接:
http://www.asual.com/jquery/address/docs/

You can make it indexable by Google (#!) by adding:
$.address.crawlable(true);
Here is a link I found helpful for jQuery Address methods:
http://www.asual.com/jquery/address/docs/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文