简体   繁体   English

使用pushState在AJAX网站上创建“链接”会产生404

[英]Using pushState to create “links” on an AJAX website produces 404s

I currently have my website set up with a single html file that uses on-click javascript functions to display various sections of the site differently as the user navigates around. 目前,我的网站设置有一个HTML文件,该HTML文件使用点击式JavaScript函数在用户浏览时以不同方式显示网站的各个部分。 The main page contains an audio player that I want to keep playing as the user navigates, which is why I've chosen the AJAX route. 主页包含一个音频播放器,我想在用户导航时继续播放它,这就是为什么我选择了AJAX路由。

I've implemented pushState across the site to produce URLs for each section that the user navigates to. 我已经在整个网站上实现了pushState,以为用户导航到的每个部分生成URL。 These links don't work. 这些链接无效。

I have a couple questions regarding this: should I be using pushState to generate these URLs, or am I going about producing links the wrong way? 我对此有几个疑问:我应该使用pushState生成这些URL,还是要以错误的方式生成链接? And is there a way I can have a user reload a deeply-embedded page in the site, and then have the page constructed from the javascript somehow server-side? 有没有一种方法可以让用户重新加载站点中的深层页面,然后以某种方式在服务器端使用javascript构建页面?

EDIT: I feel like maybe if I could have all these links routed to a javascript file that then built the appropriate page, maybe that would work? 编辑:我觉得也许如果我可以将所有这些链接都路由到一个javascript文件,然后再构建相应的页面,那可能行得通吗? Not sure if that's the way to go about it. 不知道这是否是解决问题的方法。

  $('#menu a').click(function() { var page = $(this).attr('href'); history.pushState({}, '', page); $('#news').load('content/' + page + '.php'); return false; }); 

Here's an example of how the menu is running this jQuery function on click, and then using pushState to make a new URL. 这是菜单如何在单击时运行此jQuery函数,然后使用pushState创建新URL的示例。 Then, it loads in content into the #news div from the appropriate php file. 然后,它将内容从相应的php文件加载到#news div中。

Is there any way I can link to this "state" of the page? 有什么方法可以链接到页面的此“状态”? I've been reading Jose Maria Arranz Santamaria's "Single Page Interface Manifesto" , which has a lot of great info, but I'm sort of wondering what I should do next. 我一直在阅读Jose Maria Arranz Santamaria的“单页界面宣言” ,其中有很多不错的信息,但是我有点想知道下一步该怎么做。

Pushstate is not the way you should use if the links don't actually work. 如果链接实际上不起作用,则Pushstate不是您应该使用的方式。 You can use the hashes to implement this easily. 您可以使用哈希轻松实现此目的。 You the event onhashchange . 您事件onhashchange

The hashes are stored in the window.location.hash property. 哈希存储在window.location.hash属性中。 There would be no need to modify the browser history as well. 也无需修改浏览器历史记录。

Refer to onhashchange and Hashes 参考onhashchange哈希

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM