Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 3.31 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Can I load data on a web page statically, unless JavaScript is enabled?
  2. <body>
  3.   <h1>Above the fold header</h1>
  4.   <video src="fancy-cat.mp4" ... />
  5.  
  6.   <div id="below-the-fold">
  7.    <noscript>
  8.      <!-- Users with Javascript disabled get this content -->
  9.      <h2>My cat plays piano</h2>
  10.      <video src="piano-cat.mp4" ... />
  11.  
  12.      <h2>My cat knows how to use the toilet</h2>
  13.      <video src="potty-cat.mp4" ... />
  14.    </noscript>
  15.   </div>
  16.        
  17. <script src="http://www.strictly-software.com/scripts/downloads/encoder.js"></script>
  18.   <script>
  19.     $(window).load(function() {
  20.       // Copy the 'noscript' contents into the DOM
  21.       $("noscript").each(function() {
  22.         $(this).after(Encoder.htmlDecode($(this).html()));
  23.       });  
  24.     });
  25.   </script>
  26. </body>
  27.        
  28. /
  29.     page1.jsp
  30.     fragment1_1.jsp
  31.     fragment1_2.jsp
  32.     page2.jsp
  33.     fragment2_1.jsp
  34.     fragment2_2.jsp
  35.     ...
  36.        
  37. <%!
  38.   // Define a method to help us import fragments into the current page.
  39.   // Conditional import of fragment based on isJSEnabled
  40.   void myImport (String fragment, boolean isJSEnabled, HttpServletResponse res) {
  41.     if (!isJSEnabled) {
  42.       // output fragment contents directly to response
  43.       String contents = // get contents of fragment
  44.       res.getWriter().write(contents);
  45.     }
  46.   }
  47. %>
  48.  
  49. <%
  50.   // How to work out if JS is enabled on the server-side?
  51.   // Not sure it can be done. So need to be told by the browser somehow.
  52.   // Maybe a request parameter. So if param not present, assume no JS.
  53.   boolean isJSEnabled = (null != req.getParameter("js"));
  54. %>
  55.  
  56. <html>
  57. <head>
  58. <script>
  59. // Try and redirect to JS version of page as soon as possible,
  60. // if we're not already using the JS version of the page.
  61. // This code does not take into account any existing request parameters for
  62. // the sake of brevity.
  63. // A browser with JS-enabled that was incrementally downloading and parsing
  64. // the page would go to the new URL.
  65. if (window.location.href.indexOf("js=true") < 0) {
  66.   window.location.href += "?js=true";
  67. }
  68. </script>
  69. </head>
  70. <body>
  71.   <h1 class="fragment_header" data-fragment-id="fragment1_1">Fragment 1</h1>
  72.  
  73.   <div>
  74.   <%
  75.     // Conditionally import "fragment1_1".
  76.     myImport("fragment1_1", isJSEnabled);
  77.   %>
  78.   </div>
  79.  
  80.   <h1 class="fragment_header" data-fragment-id="fragment1_2">Fragment 2</h1>
  81.  
  82.   <div>
  83.   <%
  84.     // Conditionally import "fragment1_2".
  85.     myImport("fragment1_2", isJSEnabled);
  86.   %>
  87.   </div>
  88.  
  89.   <script>
  90.     // For each fragment header, we attach a click handler that loads the
  91.     // appropriate content for that header.
  92.     $(".fragment_header").click(function (evt) {
  93.       var header = $(evt.target);
  94.       // Find the div following the header.
  95.       var div = header.next("div");
  96.       if (div.children().length < 1) {
  97.         // Only load content if there is nothing already there.
  98.         div.load(header.attr("data-fragment-id") + ".jsp");
  99.       }
  100.     });
  101.  
  102.     $("a").click(function (evt) {
  103.       // Stop the browser handling the link normally.
  104.       evt.preventDefault();
  105.  
  106.       // Rudimentary way of trying to ensure that links clicked use the
  107.       // js=true parameter to keep us is JS mode.
  108.       var href = anchor.attr("href");
  109.       var isLocalHref = // logic to determine if link is local and should be JS-ified.
  110.       if (isLocalHref) {
  111.         href = href + "?js=true";
  112.       }
  113.       window.location.href = href;
  114.     });
  115.   </script>
  116. </body>
  117. </html>