On my site, I have a jquery function that retrieves data from another (secure) server as soon as the page loads. Using a jsonp call, I am currently loading this data after a document ready event:
<script type="text/javascript"> $(document).ready(function () { $.getJSON(_secureHost + '/base/members/current.aspx?callback=?', function (data) { initPage(data); }); }); </script>
What I don't like about the above call is that jsonp can actually be output before the document ready event, thereby slowing down the page loading. Therefore, if I enable jquery inside the page (i.e. without referring to the use of the script tag), then the following code works fine, and the page loads faster:
<script type="text/javascript"> $.getJSON(_secureHost + '/base/members/current.aspx?callback=?', function (data) { $(document).ready(function () { initPage(data); }); }); </script>
But including jquery on every page is 23k overhead that I would like to avoid. How can I check if jquery is loaded, and only the excecute initPage () function when loading jquery?
Edit: To be more precise, I need to check several times if jquery is loaded, and then output the event. A timer task may be a solution.
Solution: I created a preinit that performs jquery validation. My page loading could not be faster :). Thanks to all!
function preInit() { // wait until jquery is loeaded if (!(typeof jQuery === 'function')) { window.setTimeout(function () { //console.log(count++); preInit(); }, 10); // Try again every 10 ms.. return; } $.getJSON(_secureHost + '/base/members/current.aspx?callback=?', function (data) { $(document).ready(function () { initPage(data); }); }); }
AyKarsi
source share