Jquery地址:无法处理传入的深层链接?
我在我的页面上使用这个基于 AJAX 的加载插件,老实说,它可以很好地处理所有事情,除非我尝试捕获其中没有哈希片段的传入链接。
地址栏
传入链接-> hostname.com/path/
用户导航到另一个ajax状态-> hostname.com /path/ #hash
在这里,我希望插件不要将路径包含到地址栏中,因为哈希代表了这一点。
AJAX 获取
传入链接-> hostname.com/path/
用户导航到另一个ajax状态->主机名.com/路径/
实际请求确实加载正确的状态。
有什么办法让它不将路径添加到地址栏吗?
例如
主机名/#web-development
而不是:
hostname/web-development/#web-development
点击处理
$('a.internalLink').live('点击', function(event) {
event.preventDefault(); 点击链接 = $(this); $.address.value(clickedLink.attr('href').replace(base,'')); });
I am using this plugin with AJAX based loading on my page, which handles everything pretty well to be honest, except when i try to catch incoming links that do not have a hash fragment in them.
Address Bar
incoming link-> hostname.com/path/
user navigates to another ajax state-> hostname.com /path/ #hash
Here i am expecting the plugin not to include the path into the address bar because the hash is representing this.
AJAX GET
incoming link-> hostname.com/path/
user navigates to another ajax state-> hostname.com/path/
The actual request does load the correct state.
is there any way to have it not add the path to the address bar?
e.g
hostname/#web-development
instead of:
hostname/web-development/#web-development
Click handling
$('a.internalLink').live('click', function(event) {
event.preventDefault();
clickedLink = $(this);
$.address.value(clickedLink.attr('href').replace(base,''));
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该捕获锚标记被单击的事件,并使用 event.preventDefault( );
您可能还对新的 HTML 5 附加功能感兴趣,它允许操纵浏览器历史记录;即您可以使用 AJAX 来更新页面,但仍然更改地址。请参阅此处了解更多详细信息(Mozilla 开发人员中心)。
You should capture the event of the anchor tag being clicked on, and prevent the default action occuring using event.preventDefault();
You may also be interested in the new HTML 5 additions, which allow manipulation of the browser history; i.e you can use AJAX to update the page, but still changing the the address. See here for more details (Mozilla Developer Center).
我通过将任何未散列的 URL 重定向到散列的等效项来规避这个问题,如下所示:
它不是那么优雅,也许有更好的解决方案,但它有效。
I have circumvented the problem by redirecting any unhashed URLS to the hashed equivalent as such:
It's not that elegant, maybe there's a better solution, but it works.