禁用 JavaScript 时的 Ajax 搜索解决方法
我的网站上有一个搜索页面。搜索从外部源的几个(最终是几个)API 中提取。有时,搜索可能需要长达 5 秒的时间。 因此,我想至少使用加载 gif 来加载我的搜索页面,并让 AJAX 开始提取数据,一次显示一点。 (类似于 http://gamespot.com 虽然这是一个坏例子,因为搜索在禁用 JS 的情况下不起作用)
当然,我必须考虑那些关闭了 Javascript 的用户,所以对于他们来说,我只是让 PHP 进行搜索,他们将不得不忍受等待。
实现这一点的最佳方法是什么?如果我使用 ,那么所有用户仍然必须等待 5 秒的 PHP 部分加载。
我是否可以让搜索表单根据用户的 JS 状态将他们发送到不同的页面?
I have a search page on my site. The search pulls from a couple (eventually a few) API from external sources. Sometimes a search can take up to 5 seconds.
Because of this, I would like to load my search page at least with a loading gif, and let AJAX begin to pull the data, showing a bit at a time. (similar to http://gamespot.com although this is a bad example since the search doesn't work with JS disabled)
Of course I have to consider the users who have turned Javascript off, so for them I'd just let PHP do the search and they'll have to bear with the wait.
What is the best way to implement this? If I use <noscript>
, then all users still have to wait for the 5 second PHP portion to load anyways.
Would I just have the search form send the user to different pages depending on their JS status?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许
noscript
部分定义了一个iframe
来加载长时间 PHP 查询的结果?如果您的用户来到您的页面,然后发送表单,这绝对是最好的方法。例如:
HTML:
JavaScript:
...其中
hookEvent
是一个使用addEventListener
或attachEvent
(在 IE 上)的函数。离题:
hookEvent
的事情,就像很多这样的事情一样,如果你使用像 jQuery、原型、YUI、关闭 或 < a href="http://en.wikipedia.org/wiki/List_of_JavaScript_libraries" rel="nofollow">其他任何一个。例如,使用 jQuery:Perhaps have the
noscript
part define aniframe
that loads the results from the long-duration PHP query?If you have the users coming to your page, and then sending the form, that's absolutely the best way to go. E.g.:
HTML:
JavaScript:
...where
hookEvent
is a function that usesaddEventListener
orattachEvent
(on IE).Off-topic: The
hookEvent
thing, like a lot of this stuff, is easier if you use a library like jQuery, Prototype, YUI, Closure, or any of several others. For instance, with jQuery:如果没有 JavaScript,您将需要将数据发布到服务器并在页面上执行完整的回发(刷新)。就像过去的美好时光一样。 ;)
Without JavaScript, you will need to post the data to the server and perform a full postback (refresh) on the page. Just like the good ol' days. ;)
不,你应用你的js代码(如果我理解正确的话自动完成?)直到一个输入字段。将 Javascript 想象成一个扩展器。如果禁用 js,则不会在输入字段上扩展自动完成功能。你可以在其中写一些文字,说,伙计,打开 js,否则这将是一个很长的搜索。如果js打开,则隐藏文本
no you apply your js code (autocomplete if i understoof right?) up to an input field. Think of Javascript like an extender. If js is disabled, no autocomplete is extended on the input field. You may put some text, where you say, dude, turn on js otherwise this will be a long search. And if js is on, hide the text
渐进增强:
首先构建它,使 PHP 版本能够正常工作。这是所有人都可以访问的。然后,添加 javascript,以便在可用时在幕后执行 ajax 请求以获取内容并更新当前页面。
将这本书视为关于该主题的简单而精彩的读物:
防弹 Ajax
Progressive enhancement:
Build it so the PHP version works, first and foremost. This is accessible to all. Then, add javascript so that, if available, it performs ajax requests behind the scenes to grab the content and update the current page.
See this book as a simple, great read on the subject:
Bullet Proof Ajax