Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除非启用了JavaScript,否则我可以静态加载网页上的数据吗?_Javascript_Ajax_Pageload - Fatal编程技术网

除非启用了JavaScript,否则我可以静态加载网页上的数据吗?

除非启用了JavaScript,否则我可以静态加载网页上的数据吗?,javascript,ajax,pageload,Javascript,Ajax,Pageload,假设我有一个单页面站点,我想等待加载折叠下面的任何内容(使用ajax),直到用户单击链接转到同一页面上的内容。但是,我不希望禁用JavaScript的用户看不到页面下方的任何内容。使用ajax加载内容的全部原因是,初始页面加载速度更快,因此无论采用哪种方式加载内容,然后隐藏直到用户单击为止都是毫无意义的。我确信只有启用了JavaScript才可以加载内容,但如果浏览器中启用了JavaScript,是否可以不加载特定的“静态”或服务器端生成的内容 如果浏览器中启用JavaScript,有没有办法阻

假设我有一个单页面站点,我想等待加载折叠下面的任何内容(使用ajax),直到用户单击链接转到同一页面上的内容。但是,我不希望禁用JavaScript的用户看不到页面下方的任何内容。使用ajax加载内容的全部原因是,初始页面加载速度更快,因此无论采用哪种方式加载内容,然后隐藏直到用户单击为止都是毫无意义的。我确信只有启用了JavaScript才可以加载内容,但如果浏览器中启用了JavaScript,是否可以不加载特定的“静态”或服务器端生成的内容


如果浏览器中启用JavaScript,有没有办法阻止特定的静态内容在初始页面加载时被服务器加载?< /强>

< p>您可以考虑使用标签。


P>可选的,如果“折叠下面的内容”真的是图像/视频重,你可能只想考虑内容。

你可以考虑使用标签。


可选地,如果“折叠下面的内容”真的是图像/视频重,您可能只想考虑内容。

< P>如果您想避免在有JS启用的客户端的情况下加载数据,那么您可能需要结合服务器端和客户端技术。 这可以作为一个粗略的指南披露-我没有测试过这些

例如,如果您的站点结构如下所示:

/
    page1.jsp
    fragment1_1.jsp
    fragment1_2.jsp
    page2.jsp
    fragment2_1.jsp
    fragment2_2.jsp
    ...
然后page1.jsp可能看起来像这样(如果您不知道jsp和jQuery,很抱歉,但这大部分都是伪代码):


//尝试尽快重定向到页面的JS版本,
//如果我们还没有使用JS版本的页面。
//此代码不考虑任何现有的请求参数
//为了简洁起见。
//启用JS的浏览器,正在增量下载和解析
//页面将转到新的URL。
if(window.location.href.indexOf(“js=true”)<0){
window.location.href+=“?js=true”;
}
片段1
片段2
//对于每个片段头,我们附加一个加载
//该标题的适当内容。
$(“.fragment_header”)。单击(函数(evt){
var头=$(evt.target);
//查找标题后面的div。
var div=标题。下一个(“div”);
if(子类()长度<1){
//仅在没有内容的情况下加载内容。
load(header.attr(“数据片段id”)+“.jsp”);
}
});
$(“a”)。单击(函数(evt){
//停止浏览器正常处理链接。
evt.preventDefault();
//尝试确保单击的链接使用
//js=使我们保持一致的真实参数是js mode。
var href=anchor.attr(“href”);
var isLocalHref=//用于确定链接是否是本地的并且应该被JS化的逻辑。
if(IsLocalRef){
href=href+“?js=true”;
}
window.location.href=href;
});
浏览page1.jsp的用户最初会获得静态版本,尽管启用JS的浏览器会尽快切换到启用JS的版本


当我们将单击处理程序附加到所有链接时,我们可以控制下一页的加载。如果在某个时候关闭JS,JS=true参数将不会附加到每个
href
,用户将恢复到静态页面。

如果要避免在启用JS的客户端中加载数据,则可能必须结合服务器端和客户端技术

这可以作为一个粗略的指南披露-我没有测试过这些

例如,如果您的站点结构如下所示:

/
    page1.jsp
    fragment1_1.jsp
    fragment1_2.jsp
    page2.jsp
    fragment2_1.jsp
    fragment2_2.jsp
    ...
然后page1.jsp可能看起来像这样(如果您不知道jsp和jQuery,很抱歉,但这大部分都是伪代码):


//尝试尽快重定向到页面的JS版本,
//如果我们还没有使用JS版本的页面。
//此代码不考虑任何现有的请求参数
//为了简洁起见。
//启用JS的浏览器,正在增量下载和解析
//页面将转到新的URL。
if(window.location.href.indexOf(“js=true”)<0){
window.location.href+=“?js=true”;
}
片段1
片段2
//对于每个片段头,我们附加一个加载
//该标题的适当内容。
$(“.fragment_header”)。单击(函数(evt){
var头=$(evt.target);
//查找标题后面的div。
var div=标题。下一个(“div”);
if(子类()长度<1){
//仅在没有内容的情况下加载内容。
load(header.attr(“数据片段id”)+“.jsp”);
}
});
$(“a”)。单击(函数(evt){
//停止浏览器正常处理链接。
evt.preventDefault();
//尝试确保单击的链接使用
//js=使我们保持一致的真实参数是js mode。
var href=anchor.attr(“href”);
var isLocalHref=//用于确定链接是否是本地的并且应该被JS化的逻辑。
if(IsLocalRef){
href=href+“?js=true”;
}
window.location.href=href;
});
浏览page1.jsp的用户最初会获得静态版本,尽管启用JS的浏览器会尽快切换到启用JS的版本


当我们将单击处理程序附加到所有链接时,我们可以控制下一页的加载。如果JS在某个时候被关闭,JS=true参数将不会附加到每个
href
,用户将恢复到静态页面。

这会阻止浏览器解析内容吗?这是个好问题。这可能取决于浏览器。这并不难测试。您可以创建一个内部带有图像的页面
/
    page1.jsp
    fragment1_1.jsp
    fragment1_2.jsp
    page2.jsp
    fragment2_1.jsp
    fragment2_2.jsp
    ...
<%!
  // Define a method to help us import fragments into the current page.
  // Conditional import of fragment based on isJSEnabled
  void myImport (String fragment, boolean isJSEnabled, HttpServletResponse res) {
    if (!isJSEnabled) {
      // output fragment contents directly to response
      String contents = // get contents of fragment
      res.getWriter().write(contents);
    }
  }
%>

<%
  // How to work out if JS is enabled on the server-side?
  // Not sure it can be done. So need to be told by the browser somehow.
  // Maybe a request parameter. So if param not present, assume no JS.
  boolean isJSEnabled = (null != req.getParameter("js"));
%>

<html>
<head>
<script>
// Try and redirect to JS version of page as soon as possible,
// if we're not already using the JS version of the page.
// This code does not take into account any existing request parameters for
// the sake of brevity.
// A browser with JS-enabled that was incrementally downloading and parsing
// the page would go to the new URL.
if (window.location.href.indexOf("js=true") < 0) {
  window.location.href += "?js=true";
}
</script>
</head>
<body>
  <h1 class="fragment_header" data-fragment-id="fragment1_1">Fragment 1</h1>

  <div>
  <%
    // Conditionally import "fragment1_1".
    myImport("fragment1_1", isJSEnabled);
  %>
  </div>

  <h1 class="fragment_header" data-fragment-id="fragment1_2">Fragment 2</h1>

  <div>
  <%
    // Conditionally import "fragment1_2".
    myImport("fragment1_2", isJSEnabled);
  %>
  </div>

  <script>
    // For each fragment header, we attach a click handler that loads the
    // appropriate content for that header.
    $(".fragment_header").click(function (evt) {
      var header = $(evt.target);
      // Find the div following the header.
      var div = header.next("div");
      if (div.children().length < 1) {
        // Only load content if there is nothing already there.
        div.load(header.attr("data-fragment-id") + ".jsp");
      }
    });

    $("a").click(function (evt) {
      // Stop the browser handling the link normally.
      evt.preventDefault();

      // Rudimentary way of trying to ensure that links clicked use the
      // js=true parameter to keep us is JS mode.
      var href = anchor.attr("href");
      var isLocalHref = // logic to determine if link is local and should be JS-ified.
      if (isLocalHref) {
        href = href + "?js=true";
      }
      window.location.href = href;
    });
  </script>
</body>
</html>