jQuery 深度链接 Web 应用程序
问题...我想构建一个具有深层链接的单页 Web 应用程序(点击时 url 会更改为用户加载的位置),以便用户可以共享链接、书签等...
jQuery BBQ 插件的问题是它要求 href 中包含哈希值,例如 href="#/photos/1"
这意味着我需要确保应用程序中的每个 URL 都以哈希值开头。此外,这意味着非 JS 浏览器将无法工作,因为 URL 都是经过哈希处理的。
有没有一种优雅的方法来完成一个具有深层链接的单页 Web 应用程序,可以无缝地用于 JS 和 Java 语言。非 JS 浏览器,并且需要较少的代码更改?
谢谢
Question... I want to build a one page web app that has deep linking (the url changes on click to where the user is loading) so the user can share links, bookmark, etc...
The issue with the jQuery BBQ plugin is that it requires the href to have a hash in it like href="#/photos/1"
That means I need to make sure every URL in my app starts with a hash. Also, it means non-JS browsers won't work since the URLs are all hashed.
Is there an elegant way to accomplish a one page web app with deep linking that works seamlessly for JS & Non-JS browsers, and requires less code changes?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用可同时适用于 JS 和不支持 JS 的浏览器的单一 URL 结构,您可以检查浏览器是否支持 JS 并相应地更改导航链接。如果浏览器支持/已启用 JS,请添加哈希并使用
window.location.hash
检索页面。示例 URL:site.com/#photos/1
如果不支持 JS,您的 URL 将如下所示:
site.com/photos/1
这样您只需要将
#
添加到 URL。这很容易做到,无论是在脚本本身中,还是在页面加载后使用 jQuery 更改页面上的链接。您可以使用(假设 Apache/PHP)Apache 的 ModRewrite 将 URL 重写为:site.com/index.php?p=photos&show=1
假设网站第一次加载,并且浏览器不支持 JS,加载应以正常方式加载的任何内容,具体取决于(在本例中)“page”和“show”变量。如果它确实支持 JS,您可以像这样存储变量:
加载页面后,您启动 jQuery 并提取导航数据:
并执行任何需要执行的操作来显示正确的页面。
当您单击新链接时,您可以使用各种 jQuery 插件进行哈希导航,或者当没有 JS 且链接中没有哈希时,页面将简单地以正常方式加载。
为了防止代码重复,您需要以这样的方式设置页面,以便它们可以通过 jQuery 以正常方式和轻松显示。假设您有这个照片页面,您可以在不使用 JS 的情况下浏览时包含“photos.php”,并使用 jQuery 使用相同的文件:
因为“photos.php”将看到完全相同的
$_GET
变量,输出将是相同的。不过,您将会得到一些重叠。如果您想显示/发布表单,例如,您可能会以与通过 jQuery 处理此方式不同的正常方式进行处理。处理表单数据并显示更新页面的正常方式。因此,对于“photos.php”,您需要完整输出。但是,当使用 jQuery 时,您只想要一个结果,例如“完成”,并更改页面而不完全重新加载它。这可以通过向
$.get
(或$.post
)函数添加更多变量来解决。您可能想研究 (PHP) MVC 框架。使用 MVC 设置时,您可以将站点的所有逻辑(导航/数据库查询等)与视图(即站点上显示的内容)分开。它使得使用相同逻辑变得非常容易,但是单独视图,在这种情况下,一个具有正常导航的站点和一个具有基于 jQuery 导航的站点。
最后你必须问自己是否值得经历这些麻烦。该网站是为谁服务的?这些人真的使用没有 JavaScript 的浏览器吗?真的有必要拥有一个不依赖 JS 的网站吗?如今,大多数人都在使用可以很好地处理 JS 的浏览器,人们出于“安全原因”禁用它的时代早已一去不复返了。维护网站的两个版本可能会占用大量时间和资源(当然取决于大小),因此可能不值得。在这种情况下,更容易简单地显示一条消息或一个精简且简化的网站,表明用户应该进入 21 世纪以使用功能齐全的网站。
If you want to use a single URL structure that could work for both JS and non-JS capable/enabled browsers, you can check if the browser supports JS and change the navigation links accordingly. If the browser does support/has enabled JS, add a hash and retrieving the page using
window.location.hash
. Example URL:site.com/#photos/1
If it doesn't support JS your URL would look like:
site.com/photos/1
This way you only need to add a
#
to the URL. That's easily to do, either in the script itself, or change the links on the page using jQuery after the page has been loaded. You can use (assuming Apache/PHP) Apache's ModRewrite to rewrite the URL to something like:site.com/index.php?p=photos&show=1
Say the site loads for the first time and the browser does not support JS, load whatever should be loaded the normal way, depending on (in this case) the "page" and "show" variables. If it does support JS, you could store the variables like this:
and after the page is loaded you fire up jQuery and extract the navigation data:
and do whatever needs to be done to show the correct page.
When you click new links you can use the various jQuery plugins for the hash navigation, or the page will simply load in the normal way when there's no JS and thus no hash in the links.
To prevent code duplication you need to setup your pages in such a way that they can be easily displayed the normal way and via jQuery. Say you have this photos page, you could include a "photos.php" when browsing without JS and use the same file using jQuery:
Since the "photos.php" will see the exact same
$_GET
variables, the output will be the same.You will get some overlap though. If you want to display/post a form e.g. you'll probably handle that differently the normal way opposed to when you handle this via jQuery. The normal way you'd process the form data, and display an updated page. So in the case of "photos.php" you'll want to full output. But when using jQuery you only want a result that e.g. says "done" and change the page without reloading it entirely. This can be solved by adding a few more variables to the
$.get
(or$.post
) functions.You might want to look into (PHP) MVC frameworks. When using an MVC setup you can separate all the site's logic (navigation/database queries/etc) from the view (i.e. what is displayed on the site). It makes it really easy to use the same logic, but separate views for, in this case, a site with normal and one with jQuery-based navigation.
In the end you must ask yourself if it's worth going through all this trouble though. Whom's the site for? Do these people really use browsers without JavaScript? Is it really necessary to have a site available that doesn't rely on JS? Nowadays mostly everyone is using a browser that can handle JS just fine, and the time that people would disable it for 'security reasons' is long gone. Maintaining both versions of a site might take up a lot of time and resources (depending on the size of course), so it might just not be worth it. In that case it's easier to simply display a message or a stripped down and simplified site stating that the user should step into the 21st century to use the fully functional site.