just for fun...
Is it possible to have a .js file that loads jquery from a remote site and then utalizes jquery inside the .js file...
so say site: www.sitea.com... doesn't use jquery. but they find this awesome script on site b that they want to use in their page... so they do a:
<script src="http://siteb.com/cool-script-location/cool-script.js" type="text/javascript" />
now... cool script uses lots of jqu开发者_运维技巧ery... but since site a doesn't use jquery... could i do something like this in cool-script.js:
document.writeln('<script src="http://siteb.com/jquery-location/jquery-v.v.v.min.js type="text/javascript"><script');
document.writeln('<div class="test">test</div>');
$(document).ready(function() {
$('.test').click(function() {
<!-- cool stuff goes here -->
});
})
This obviously doesn't work in practice or i wouldnt be asking.
It would probably be best if you simply checked for the existence of the jQuery
object first and if it didn't exist, either threw an error or displayed an alert to tell the webmaster they need jQuery version 1.x.x for the script to work. Also, on the page where they can download your script, you should make it very obvious that your script is dependent on jQuery.
There are multiple reasons for this, the biggest been that developers don't like it when scripts use resources without their knowledge, and also because loading external javascript via javascript is notoriously unreliable.
精彩评论