AJAXify 网站
我有正当理由去做我试图解释的事情。 我有一个现有的网站,比如 abc.com,它有常规页面等,所有内容都是用 php 编写的。 现在我想对网站进行 AJAX 化,即当用户单击链接时,它应该使用 AJAX 获取链接并替换页面内容。 这是简单的部分,我可以使用 jQuery get 函数来实现它。
现在,当用户为页面添加书签时,问题就出现了。 我可以使用哈希标签来指定用户是否在另一个页面上,但不是使用 javascript 再次获取新页面,而是可以在调用页面时直接使用 PHP 获取它。
您能给我一个如何实现上述目标的概要吗? 此功能与 Facebook 的功能类似。
感谢您的时间。
I have legitimate reasons to do what I am trying to explain. I have an existing site say abc.com which has regular pages etc. everything written in php. Now I would like to AJAXify the site i.e. when a user clicks on a link, it should fetch the link using AJAX and replace the page contents. This is the easy part and I can achieve it using jQuery get function.
Now the problem comes when the user bookmarks the page. I can use hash tags to specify if the user is on another page, but instead of using javascript to fetch the new page again, is it possible to fetch it directly using PHP when the page is called.
Can you please give me an outline on how to achieve the above. This functionality is similar to what Facebook has.
Thankyou for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个相当简单的过程:(1) 解析哈希标签,(2) 像平常一样通过 Ajax 加载内容。
如果您在用户单击页面时加载更多内容,请确保始终正确修改哈希标签以反映页面上的内容。
这是一个简单的示例供您使用。 单击名称并记下井号标签。 相关的 JavaScript 看起来像这样:
It's a fairly simply process of (1) Parsing the hash tag, and (2) Loading the content via Ajax as you normally would.
If you load more content when the user clicks on the page, just be sure to always correctly modify the hash tag to reflect what's on the page.
Here's a quick example to play around with. Click on a name and note the hash tag. The relevant Javascript looks like this :
答案是否定的,你无法获取URL哈希服务器端的值。 请参阅如何从服务器端获取网址哈希 (#) 。
您必须在客户端获取哈希值并发出额外的请求。
The answer is no, you can't get the value of the URL hash server side. See How to get Url Hash (#) from server side.
You'll have to get the hash value client-side and make an extra request.
jQuery History 是我的首选。 可以在这里找到: http://www.balupton.com/projects/jquery-history / 提供跨浏览器支持,绑定到哈希值,重载哈希值,等等。
它还有一个名为 jQuery Ajaxy 的 Ajax 扩展,允许它轻松地将您的网页升级为适当的 Ajax 应用程序,而无需服务器端更改并保持 SEO 和 JS-Disabled 友好: balupton.com/projects/jquery-ajaxy/" rel="nofollow noreferrer">http://www.balupton.com/projects/jquery-ajaxy/
这是 http://wbhomes.com.au/ 和 http://www.balupton.com
总体而言,它们都有完善的文档、支持且功能丰富。 他们还在这里赢得了赏金问题 如何显示 Ajax 请求在网址中?
jQuery History is my preferred choice. It can be found here: http://www.balupton.com/projects/jquery-history/ Provide cross browser support, binding to hashes, overloading hashes, all the rest.
There is also an Ajax extension for it called jQuery Ajaxy, allowing it to easily upgrade your webpage into a proper Ajax application without need for server side changes and remaining SEO and JS-Disabled friendly: http://www.balupton.com/projects/jquery-ajaxy/
This is the solution chosen by such sites as http://wbhomes.com.au/ and http://www.balupton.com
Overall they are both well documented, supported and feature rich. They've also won a bounty question here How to show Ajax requests in URL?
那么您可以使用这些工具之一或自己推出。 使用 window.location.hash 以及其他技巧。
Well you can use one of these tools or roll your own. Uses window.location.hash along with other tricks.
我最近写了一篇关于这个问题的文章。 这篇文章是教程风格。 使用哈希更改事件,我展示了如何向 url 添加参数,这可以触发具有无限数量参数的 ajax 请求,因此以相当可扩展的方式运行。
您可以在 http:// andresgallo.com/2012/06/08/ajaxifying-the-web-the-easy-way/
第一部分介绍如何响应哈希链接,而第二部分介绍如何发送哈希 URL 中的数据到 ajax 请求,因此允许您从 url 中的哈希标签中执行几乎任何您想要的操作。
I wrote an article recently about this exact issue. The article is tutorial style. Using the hash change event, im showing how to add parameters to the url, which can trigger ajax requests with infinite number of parameters, therefore functioning in a pretty scaleable manner.
You can find more about it here at http://andresgallo.com/2012/06/08/ajaxifying-the-web-the-easy-way/
Part I covers how to respond to the hash links, while part II covers how to send the data in the hash url to an ajax request therefore allowing you to do pretty much anything you want out of the hash tags in your url.
做到这一点的最佳方法是让一个 index.php 根据其后的 URL 部分加载所有其他页面。 例如:
http://www.example.com/index.php/reports /employees/hoursWorked
在这种情况下,index.php 将运行,它可以看到所请求的是工作时间报告,并加载该内容。 问题是,如果 index.php 然后使用 AJAX 加载所有其他内容,则浏览器中的 URL 永远不会改变。
解决该问题的一种方法是在包含该形式 URL 的每个页面上放置一个“链接到此页面”链接,以供用户添加书签。
The best way to do this is to have one index.php that loads all other pages based on the URL parts after it. For instance:
http://www.example.com/index.php/reports/employees/hoursWorked
In this case index.php will run, it can see what's being requested is is the hours worked report, and load that content. The problem is, if index.php then loads all other content after that using AJAX, the URL in the browser will never change.
One way around that problem would be to put a "Link to this page" link on every page that contains that form of URL for users to bookmark.