/ Published in: JavaScript
After having trouble with xss and .getJSON() I wrote this. Solved my problems so here you go.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$.fn.jsonp = function(params) { var qs = '?callback='+params.callback; if (params.data) { for(var v in params.data) { qs += ('&'+v+'='+params.data[v]); } } var url = params.url+qs; var time = new Date().getTime().toString(); url += ('&'+ time); var tag = $('<script type="text/javascript" src="'+url+'" id="'+time+'">'); var scriptTag = $(this).append(tag); return $(tag); } function boo(res) { alert('loaded!') } $(document).ready(function(){ params = { callback:'boo', data:{ q:'test' }, url:'http://somesite.com/jsonp' }; $('body').jsonp(params); });
URL: http://www.digitalscientists.com