1

I have a webpage with a lot of javscript functions. It was a requirement for the job to have a single html file and call all the sections inside the same page and replace the "old content" with the new one without leaving the page. (Please don't tell me if it was a good idea or not.)

So, all of them are necesary for the site to work, but it is not necesary to load them at the beggining. Is there any form to load and call functions when clicked a button?

I will appreciate a lot your help.

10
  • 1
    what does "living the page" mean? Commented Aug 21, 2012 at 23:15
  • Can you put all the javascript in an external file and compress it? How much javascript are you talking about? It could be that it is faster to load everything at once instead of multiple requests. Commented Aug 21, 2012 at 23:15
  • Dave: leaving the page means change from index.html to other.html Sorry for the mistake Commented Aug 21, 2012 at 23:16
  • Some: 500kb, but it is a mobile site, so it is too much for a 3g connection Commented Aug 21, 2012 at 23:17
  • 1
    Depending on how you write your files, you can load the bare minimum in the first file, and then have it load another file (by inserting a script-tag in the document.head) with the "extra" functions. Since it is for a mobile site you should look into minification and compression. That will save you a lot of bandwith. Commented Aug 21, 2012 at 23:25

1 Answer 1

1

I suggest that you put the javascript in two files.

In file 1 you put the bare minimum and in file 2 the rest. You can load file 2 when you want by doing something like this:

var script = document.createElement('script');
script.type = 'application/javascript';
script.src = 'name_of_script_2.js';
document.head.appendChild(script);

You should also look into minification and compression. For example Googles closure compiler. (start with just white-space-optimization and then try simple optimization. The advanced optimization requires special things to work) And make sure you gzip your files to make them as small as possible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.