[英]Make a #-tag javascript link word for non javascript
Using the following tutorial I want my website to use AJAX to load the content (but also want to be able to use the back button etc. etc): http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp 使用以下教程,我希望我的网站使用AJAX加载内容(但也希望能够使用后退按钮等): http : //www.queness.com/post/328/a-simple- Ajax驱动-网站与- jqueryphp
Ofcourse if someone has javascript disabled the website should also work (without Ajax). 当然,如果有人禁用了javascript,则该网站也可以正常工作(不使用Ajax)。
The problem however comes when a javascript enabled user sends a link to a non javascript enabled user. 但是,当启用JavaScript的用户向未启用JavaScript的用户发送链接时,就会出现问题。 Because javascript is disabled it will not handle the #-tag correctly and will just go to the homepage (so linking directly to pages from a javascript user to non-javascript user is impossible).
由于禁用了javascript,它将无法正确处理#标签,而只会转到首页(因此,无法直接将页面从javascript用户链接到非javascript用户)。 Is there a way to resolve this issue (preferably php or htacces).
有没有一种方法可以解决此问题(最好是php或htacces)。
HTML5 gives us methods to alter the URL without refreshing the page https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries HTML5为我们提供了无需刷新页面即可更改URL的方法https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries
This means you can update something without a page refresh but still give the user a url they can bookmark or send to someone else. 这意味着您可以在不刷新页面的情况下更新某些内容,但仍为用户提供一个他们可以添加书签或发送给其他人的网址。 These urls will work without JavaScript, as long as you have pages at those locations or are catching them with mod_rewrite or similar.
只要您在这些位置有页面,或者正在使用mod_rewrite或类似内容捕获它们,这些URL就可以在没有JavaScript的情况下工作。
https://github.com/browserstate/history.js is a great little pollyfill which will use the HTML5 history stuff if the browser supports it, otherwise (Internet Explorer) it changes the hash of the url. https://github.com/browserstate/history.js是一个很棒的小插件,如果浏览器支持,它将使用HTML5历史记录,否则(Internet Explorer)它将更改URL的哈希。
Basically, three steps: 基本上,三个步骤:
<a href='about'>About us</a>
<a href='about'>About us</a>
click
events on <a>
tags and navigate to #
+ this.href
. <a>
标记上的所有click
事件,然后导航至#
+ this.href
。 So when they click the above url, you navigate to site.com/#about
instead of site.com/about
site.com/#about
而不是site.com/about
Since you code your html just as usual, the site remains fully accessible for non-js users, and, more important, for search engines' bots. 由于您照常编写html代码,因此非js用户(更重要的是搜索引擎的bot)仍然可以完全访问该网站。
In response to the comments I can suggest the following: 针对这些评论,我可以提出以下建议:
site.com
to site.com/js/
site.com
重定向到site.com/js/
<a href='about'>
is clicked, navigate to site.com/js/#about
<a href='about'>
时,导航至site.com/js/#about
<a id=about href="/about">click here</a>
for non-js users <a id=about href="/about">click here</a>
功能 Why not just build your application normally and then add the AJAX on top, rather than going the other way round and causing more work for yourself? 为什么不正常地构建您的应用程序,然后在顶部添加AJAX,而不是反过来又为您自己做更多的工作呢?
Ask yourself, why do you need AJAX page transitions? 问问自己,为什么需要AJAX页面转换? Does your app actually need them, or is it just because you've seen it on another site, like Twitter?
您的应用程序实际上需要它们吗,还是仅仅因为您在其他网站(例如Twitter)上看到过它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.