简体   繁体   English

制作非JavaScript的#标签JavaScript链接词

[英]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: 基本上,三个步骤:

  • code your "a" tags just normal: <a href='about'>About us</a> 正常编码您的“ a”标签: <a href='about'>About us</a>
  • in your javascript code, intercept all click events on <a> tags and navigate to # + this.href . 在您的JavaScript代码中,拦截<a>标记上的所有click事件,然后导航至# + this.href So when they click the above url, you navigate to site.com/#about instead of site.com/about 因此,当他们单击上面的URL时,您导航到site.com/#about而不是site.com/about
  • in your javascript code, have a timer function that reads the hash value form the current location and loads a corresponding url (with # removed) via ajax 在您的JavaScript代码中,有一个计时器函数,该函数从当前位置读取哈希值并通过ajax加载相应的url(删除了#)

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: 针对这些评论,我可以提出以下建议:

  • redirect your home page via javascript from just site.com to site.com/js/ 通过javascript将您的主页从site.com重定向到site.com/js/
  • when <a href='about'> is clicked, navigate to site.com/js/#about 单击<a href='about'>时,导航至site.com/js/#about
  • on the "js" page, have something like <a id=about href="/about">click here</a> for non-js users 在“ js”页面上,为非js用户提供类似<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.

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