0

What would be a good way to show hidden content with javascript, without having the image elements <img src="myimage.jpg"> of the hidden content load their images in google chrome or any other browsers until the content is actually shown?

hiding the content with the css rule display: none will not prevent the images from loading, and I would like to avoid using ajax calls.

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
  • Why would you do this? What if the user is at the far end of a bad connection - every time they "reveal" content, they'll first get it with no images, then have to wait for them to load in. Whereas, if you leave it alone, they'll be ready when they do switch (probably). – Damien_The_Unbeliever Jan 30 '12 at 10:04
  • @Damien_The_Unbeliever because this content gets rarely seen, but is present on every page of the website, and I would like to reduce the amount of http requests of the average viewer to a minimum. (there were around 200, I reduced them to 84. 35secs vs 9secs). I know I could use ajax, but this content requires so little html, that having an ajax request for it seems like overkill. – Timo Huovinen Jan 30 '12 at 10:09
  • You might be able to create an to load after the src is provided. Try this http://jsfiddle.net/LYMRV/ – Aram Kocharyan Jan 30 '12 at 10:17
  • @YuriKolovsky: Do you use jquery or could you do so if it solved this problem? – Chris Jan 30 '12 at 10:43
  • @Chris I could use jquery if it solved the problem, but would rather have a non-jquery solution if at all usable. I can always rewrite the jquery solution to normal javascript. – Timo Huovinen Jan 30 '12 at 10:46

4 Answers4

1

EDIT 1 as discussed in the comments, a better alternative would be to use a template. As an example I picked John Resig’s Microtemplating engine:

<div id="content_container">
    <script type="text/html" id="content">
        <div>
            <img src="..."/>
        </div>
    </script>
</div>
<button onclick="document.getElementById('content_container').innerHTML = tmpl('content', {});">show div</button>

See fiddle

EDIT 2

As the original poster commented, it's perfectly possible to grab the contents of a <script type="text/html"> element. Templating engine's not necessary:

<div id="content_container">
    <script type="text/html" id="content">
        <div>
            <img src="..."/>
        </div>
    </script>
</div>
<button onclick="document.getElementById('content_container').innerHTML = document.getElementById('content').innerHTML;">show div</button>

First Answer

(see in edits)

ori
  • 7,817
  • 1
  • 27
  • 31
  • Would the browser not be loading the images already by the time the document.ready fires? – Chris Jan 30 '12 at 10:15
  • @Chris as far as I know, it would. – Timo Huovinen Jan 30 '12 at 10:18
  • No, it triggers just before loading any assets – ori Jan 30 '12 at 10:18
  • @ori, yes, it triggers before loading any assets, but I think it executes while the assets load. – Timo Huovinen Jan 30 '12 at 10:22
  • If you want to be absolutely sure, maybe you can start by leaving the `src` blank, and putting the uri in the `title` attribute or whatever. Then in the `show` function swap those values out. – Mr Lister Jan 30 '12 at 10:22
  • @YuriKolovsky I guess you might be right. You could change all your hidden images to ``, but it's less convenient – ori Jan 30 '12 at 10:27
  • Maybe you should consider turning your hidden sections into templates, see http://stackoverflow.com/questions/170168/jquery-templating-engines – ori Jan 30 '12 at 10:29
  • @ori thanks for the idea, but I forgot to mention that this hidden content is very small, which is why I want to avoid using ajax calls for it in the first place, it's around 25-50bytes of markup, and implementing js templates would be overkill for it? I'm hating that browsers did not provide an option to not load images, like a css rule of sorts `display:none!no-need-to-even-load-images`. – Timo Huovinen Jan 30 '12 at 10:40
  • It should be noted that the original poster hasn't given any indication he is using jQuery either (its not in the tags) so you may want to check if jquery is an option before thinking too heavily about that sort of thing. – Chris Jan 30 '12 at 10:41
  • I think templates would be a nice solution. You could just wrap the children of a hidden container with a ` – ori Jan 30 '12 at 10:47
  • @ori could you please provide an example in your answer? – Timo Huovinen Jan 30 '12 at 10:54
  • @ori that is very interesting! but is it ok to put divs and unescaped html markup directly inside script tags? **edit** it looks like I missed the type=text/html of the script tag. – Timo Huovinen Jan 30 '12 at 13:30
  • 1
    Know that `` works just as good, and has no dependencies. – Timo Huovinen Jan 31 '12 at 09:53
0

First, define a CSS style:

.invisible {
    display: none;
}

Add this class to the objects in the HTML. Then anywhere in the JavaScript, simply add or remove the class, or change the display to block or float etc. In jQuery:

http://api.jquery.com/addClass/

http://api.jquery.com/show/

EDIT:

If you don't want the image to load, then use an AJAX call instead.

http://api.jquery.com/jQuery.get/

jQuery.get('myImage.jpg', function(data) {
  jQuery('.imageContainer').html(data);
});

EDIT 2:

Load the src into the img once it's needed. You could check the scroll position etc.

http://jsfiddle.net/LYMRV/

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
0

To do what you want within your requirements you could have javascript write the content when you want it displayed. So you would store your HTML in a javascript string and just use script to then insert it into the page when you want it. Its not a very nice way of doing it but it would mean that it would only load images at that point.

Alternatively you could put the HTML in but have the images pointing at nothing (or a blank placeholder, etc.) and then use script to programatically populate the image sources to the correct values when you call the show function to show the page.

Which of these you choose is probably more about readability than anything else though I would favour the second approach (just tweaking the image sources).

Chris
  • 27,210
  • 6
  • 71
  • 92
  • I could also store the content as javascript by json encoding it server side, and then present the content, but it requires me to capture the html using server side programming. – Timo Huovinen Jan 31 '12 at 08:47
0

Seems like it is possible to hide content using a script tag with type="text/html", it even prevents any images and iframes from loading in the background, for example:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
document.addEventListener('click',function(e){
    if(e.target.id=='content_show'){
        e.preventDefault();
        document.getElementById('content_visible').innerHTML = document.getElementById('content_hidden').innerHTML;//document.getElementById('content_hidden').text also works
    }
});
</script>
</head>
</body>
<img src="image1.jpg"/>
<script type="text/html" id="content_hidden">
<img src="image2.jpg"/>
<img src="image3.jpg"/>
<img src="image4.jpg"/>
</script>
<a href="#showcontent" id="content_show">Show Content</a>
<div id="content_visible"></div>
</body>
</html>

Only thing to keep in mind is to avoid placing script tags inside #content_hidden.

Now if anyone is friendly enough to point out every flaw in this method, so that we can all benefit.

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143