1

Context

I have a HTML5+CSS+JS slideshow designed to be synchronized between 50 clients in a domestic LAN with one wireless router.

Problem

Since the contents of the slides (mainly pictures) may be too heavy, I want to load them dynamically for each slide (e.g. as the client clicks a "next" button), since currently the site GETs all the files for every slide from the server at the beginning when the page is loaded, overloading the router.

In this question (another approach for the same problem) an user suggested me using AJAX to get only the DOM and then loading its contents dynamically. Nevertheless, his solution doesn't work for me, as the contents are loaded before the moment I want to.

Is this AJAX based approach correct? If so, what may I be doing wrong?

My code

slideshow.html (slideshow structure)

<html>
  <head>
    <title>My Slideshow</title>
    <script src="javascripts/slidesplayer.js"></script>
    <link rel="stylesheet" href="/stylesheets/style.css">
  </head>

  <body>    
    <div id="slides-containter">

      <div class="slide" id="slide_1">
        <!--Contents such as images, text, video and audio sources -->
      </div>

      <div class="slide" id="slide_2">
        <!--Contents -->
      </div>

      <!--A bunch of slides here-->

    </div>
    <script>
      // Here I load the slides calling a function defined in slidesplayer.js
    </script>
  </body>
</html>

slideshow-placeholder.html (loaded when I enter to the slideshow URL)

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="/path/to/ajaxSlidesScript.js"></script>
  </head>
  <body>
    <h1>Hello slides!</h1>
  </body>  
</html>

ajaxSlidesScript.js

$(document).ready(function() {
  $.ajax('/path/to/slideshow.html', {
    async : false,
    complete : function ajaxCallback(slidesDOM) {

      // Pull out the individual slides from the slideshow HTML
      $slides = $(slidesDOM.responseText).find('.slide');

      // For each one ...
      $slides.each(function prepareSlide() {

        // Store a reference to the slide's contents        
        var $slideContent = $($(this).html());  // <--- GETs all the files for this slide which I want to avoid.

        // Empty the contents and keep only the slide element itself
        var $slideWrapper = $(this).empty();

        // Attach to focus event handled by the slideware
        $slideWrapper.appendTo('body').on('focus', function injectContent() {
        // Put the content in — NOW external resources should be downloaded via GET and loaded, not before.
          $slideWrapper.append($slideContent);
        });
      });
    }
  });
});

Update: This approach won't work, as manipulating DOM object will cause the download of the resources even if you don't insert them in the DOM. You can see what I did in this question.

Community
  • 1
  • 1
Sam
  • 1,222
  • 1
  • 14
  • 45
  • 3
    This isn't actually AJAX, because you have `async` set to `false`... So your data is still loading synchronously – adamb Dec 26 '12 at 15:37

1 Answers1

0

Ajax is definitive the right technology for your problem but as far as I can see your problem is simple.

<script src="javascripts/slidesplayer.js"></script>
<link rel="stylesheet" href="/stylesheets/style.css">

With this piece of code it doesn't matter if you use ajax or not because you load the CSS file already and all images referenced in this CSS file are load directly at the beginning. In addition you should load all files asynchronous.

You can add <img> tags via JavaScript and load the images asynchronous...

Max
  • 904
  • 8
  • 15
  • Actually this is not my problem, since the file containing the link to the CSS file is not the file that is loaded at the begining (which is slideshow-placeholder.html). I tried removing the link to the CSS and the problem is still there. – Sam Dec 26 '12 at 15:46