除非启用 JavaScript,否则我可以在网页上静态加载数据吗?
假设我有一个单页网站,并且我想等待加载折叠下方的任何内容(使用 ajax),直到用户单击链接转到同一页面上的该内容。但是,我不希望禁用 JavaScript 的用户看不到首屏下的任何内容。使用 ajax 加载内容的全部原因是为了使初始页面加载速度更快,因此以任何一种方式加载内容然后隐藏直到用户单击都是毫无意义的。我确信只有在启用了 JavaScript 的情况下才可以加载内容,但是如果在浏览器中启用了 JavaScript,是否可以不加载特定的“静态”或服务器端生成的内容?
如果浏览器中启用了 JavaScript,是否有任何方法可以阻止服务器在初始页面加载时加载特定的静态内容?
Let's say that I have a single page site, and I want to wait to load any content below the fold (using ajax) until the user clicks a link to go to that content on the same page. However, I don't want users who have JavaScript disabled to just not see any content below the fold. The whole reason to load the content with ajax is so that the initial page load is faster, so having the content load either way and then hide until the user clicks would be pointless. I'm sure it's possible to only load content if JavaScript is enabled, but is it possible to not load specific "static" or server-side generated content if JavaScript is enabled in the browser?
Is there any way to prevent specific static content from being loaded by the server on initial page load at all if JavaScript is enabled in the browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑使用 noscript 标记。
然后,您可以在页面加载时使用 javascript 复制这些
标记的内容并将它们插入到 DOM 中。
noscript 标签的
innerHTML
属性将返回 HTML 编码字符串。但是您可以使用方便的 dandy Encoder 脚本将其转换为 DOM 喜欢的内容。或者,如果“首屏”内容确实包含大量图像/视频,您可能只想考虑 延迟加载 内容。
You might consider using the noscript tag.
You can then use javascript to copy the contents of these
<noscript>
tags on page load and insert them into the DOM.The
innerHTML
property of a noscript tag will return an encoded string of HTML. But you can use the handy dandy Encoder script to convert it to something the DOM will like.Alternatively, if the "below the fold" content is really image/video heavy, you might just want to consider Lazy Loading the content.
如果你想避免在客户端启用 JS 的情况下加载数据,那么你可能必须结合服务器端和客户端技术。
这可以用作粗略指南。 披露 - 我没有测试过这些!
例如,如果您的站点结构如下所示:
那么 page1.jsp 可能如下所示(抱歉,如果您不了解 JSP 和 jQuery,但这主要是伪代码):
浏览到 page1.jsp 的用户将得到最初是静态版本,尽管启用 JS 的浏览器会尽快切换到启用 JS 的版本。
当我们将点击处理程序附加到所有链接时,我们可以控制下一页的加载。如果 JS 在某个时刻关闭,js=true 参数将不会附加到每个
href
中,用户将恢复到静态页面。If you want to avoid loading data in the case of a client with JS enabled, then you might have to combine server-side and client-side techniques.
This could be used as a rough guide. Disclosure - I've not tested any of this!
For example, if your site structure looked like this:
Then page1.jsp could look like this (apologies if you don't know JSP and jQuery, but this is mostly pseudo-code anyway):
A user browsing to page1.jsp would get the static version initially, though a JS enabled browser would switch to the JS enabled version as soon as possible.
As we attach click handlers to all the links, we can control the loading of the next page. If JS is switched off at some point, the js=true parameter will not be appended to each
href
and the user will revert to a static page.