most of the time we include script tag in html head tag. like
<Head>
<script type="text/javascript" src=".5.2/jquery.min.js"></script>
</head>
I want that I don't want to include js file path rather I need to download js programmatically by jquery. like i will download js file programmatically from .5.2/jquery.min.js and check if the file can not be download or does not exist then i will download the same file from my site like www.my-site/js/1.5.2/jquery.min.js.
please help me to do it using jquery.
most of the time we include script tag in html head tag. like
<Head>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
I want that I don't want to include js file path rather I need to download js programmatically by jquery. like i will download js file programmatically from http://ajax.googleapis./ajax/libs/jquery/1.5.2/jquery.min.js and check if the file can not be download or does not exist then i will download the same file from my site like www.my-site./js/1.5.2/jquery.min.js.
please help me to do it using jquery.
Share Improve this question edited Nov 24, 2019 at 6:07 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Aug 13, 2011 at 15:33 MouMou 16.4k46 gold badges163 silver badges284 bronze badges 4- 1 Why not just download it from google all the time? And how can we help you with jquery if jquery hasn't been downloaded yet? – Amir Raminfar Commented Aug 13, 2011 at 15:34
- 6 You want to download jquery using jquery? You would have to download jquery first :) – JB Nizet Commented Aug 13, 2011 at 15:35
- 1 Dupe of stackoverflow./questions/1014203/… – Varun Madiath Commented Aug 13, 2011 at 15:40
- Maybe I'm missing something, but if you're trying to load jquery in the first place you will have to use plain old javascript to load the files until jquery has been loaded. You could probably do this using standard XHR and injecting the file contents into a script tag. Is there a reason you want to favor google's reference vs your own? Edit: jquery recursive dependency already mentioned above :) – nebs Commented Aug 13, 2011 at 15:42
2 Answers
Reset to default 4I think you want to add script programmetically. This following mechanism allows the rendering engine to immediately render and display the initial view defined in HTML while the JavaScript resources are still being loaded and executed which leads to better user experience
function loadScript(src, callback) {
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
done = false;
script.setAttribute('src', src);
script.setAttribute('type', 'text/javascript');
script.setAttribute('charset', 'utf-8');
script.onload = script.onreadstatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'plete')) {
done = true;
script.onload = script.onreadystatechange = null;
if (callback) {
callback();
}
}
}
head.insertBefore(script, head.firstChild);
}
// load the my-script-file.js and display an alert dialog once the script has been loaded
loadScript('my-script-file.js', function() { ///Loaded, add your javascript here. });
When tags are found in the HTML document the referenced script resources are downloaded and executed before the rendering engine can continue to download other resources which effectively blocks the rendering of the page below the tag. To avoid this blocking behaviour a script tag can be created via a mechanism known as a dynamic script tag injection Reference(http://www.nczonline/blog/2009/07/28/the-best-way-to-load-external-javascript/)
following code will solve your problem
<script src="//ajax.googleapis./ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/Scripts/jquery-1.5.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744224452a4563937.html
评论列表(0条)