0

I have an HTML page where some sections require large Javascript files and contain large images. These sections are initially dot displayed. Despite "display: none" the browser loads all images and Javascript. One way to solve this problem would be to move these sections into a separate file and use ajax to load it.

I want to avoid the overhead of making another request and I am thinking it would be nice to ajax-load the html from the dom or a javascript string. Is this possible?

Thanks, Ropo

ropo
  • 1,466
  • 2
  • 18
  • 29
  • Have you tried jQuery `load()` http://api.jquery.com/load/ and `.getScript()` http://api.jquery.com/jquery.getscript/ ? – Stefan Sep 06 '14 at 10:43
  • 1
    I guess this trick will work for you: http://stackoverflow.com/a/2897639/2903251 – Peter van der Wal Sep 06 '14 at 10:44
  • Thanks Peter, I have seen this before but I wanted to avoid changing the HTML. Stefan, since I only have one large Javascript to load I am probably going to load it with jQuery .getScript(). jQuery .html() seems to be a possible answer, but then everything must be quoted in the code. I think I'll go with a combination of Peter + .getScript() – ropo Sep 06 '14 at 11:28
  • Loading the HTML from the DOM will not avoid the overhead of another HTTP request. If the JS file/image is loaded only half the time, I'd consider the HTTP request an acceptable tradeoff, but YMMV. – Qantas 94 Heavy Sep 07 '14 at 09:00

1 Answers1

0

Ajax is the term giving to making HTTP requests and handling the response with JavaScript in a browser. So, by definition, no you can't.

You can store the data in a variable or the DOM, but Ajax is the part of the code you'd otherwise use that you would be replacing.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335