What's the best way to offer javascript embed that won't slow a page down?
I have a chunk of javascript that users can copy and paste to put on their sites.
I'm currently using the following code (ala WEDJE) that allows the rest of the page to load even if my script is slow or not responding.
<script type="text/javascript">
va开发者_运维百科r number = "987654321";
var key = "123abc";
(function(){
document.write('<div id="ttp"></div>');
s=document.createElement('script');
s.type="text/javascript";
s.src="http://example.com/javascripts/embed.js?" + Math.random();
setTimeout("document.getElementById('ttp').appendChild(s);",1);
})()
</script>
But that method is a few years old and so I wasn't sure if there was a more efficient way of doing the same thing that others have come up with.
I think that is still i pretty common way to do it when you need to load a file "on demand" and in similar situations.
I also think I would append it as the last child to body
though, instead of writing a new element to the DOM for the purposes of appending the script element.
1) Save the file to your source directory so loading is not dependent on web connection
2) Load the script at the bottom of the page in regular script import fashion...
<script type="text/javascript" language=JavaScript src="/scripts/ext.utils.prototype.js"></script>
OR:
3)Check out http://labjs.com/ - though be warned you may have dependency issues
精彩评论