使用 jQuery Address 进行 AJAX 深度链接
我有一个有很多页面的网站:
例如:
主页:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 url 复制粘贴到地址栏可以正常工作
后退/下一步按钮也可以使用。
# 应该在那里让它工作
任何人都知道如何使 #! 以便它可以被谷歌索引吗?
或 #/ 是同一件事吗?
基本上这对我来说在 WordPress 上工作:
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:
像这样的东西:
更新:
如果您手动加载页面(例如在链接点击时)而不是使用地址事件处理程序(例如
$.address.change(function () { .. })
),然后将上面的$.address.value(path);
替换为对path
处页面的 Ajax 调用:Something like this:
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 atpath
:您可以通过添加以下内容使其可被 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/