I’m displaying an external JavaScript file using jQuery. Is the reason “same origin policy” is not being broken because it is not an AJAX request?
Fiddle code :
HTML
<body>
<div id="toupdate">
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></script>
</div>
</body>
jQuery
$(document).ready(function() {
console.log('HTML is '+$('#toupdate').html());
});
Oh absolutely no problem here. You could reference javascript files from wherever you want. For example Google CDN provides common js files such as jQuery that you could use:
By the way that’s exactly how jQuery’s implementation of JSONP works. It uses javascript to inject a
<script>tag dynamically to the DOM pointing to some remote server side script:this remote script responds with a
Content-Type: 'application/x-javascript'response header and the following body:and on your domain you simply define the
abcfunction:and there you go: a simulation of a cross domain AJAX (I say simulation because it is not using the native XHR object but it achieves the same effect).
Now you can understand why jQuery’s JSONP implementation is limited to GET requests only => because when you inject a script tag, the browser sends only a GET request to its
srcattribute.