-
Notifications
You must be signed in to change notification settings - Fork 12
/
GET remote data (content) from url.html
44 lines (40 loc) · 1.86 KB
/
GET remote data (content) from url.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1) simple AJAX examples:
https://github.com/ttodua/useful-javascript/blob/master/AJAX-examples
2) show results parallely while you type in a field:
https://github.com/ttodua/useful-javascript/blob/master/show-results-while-typing
3) YAHOO query:
USAGE: get_remot_data("http://example.com/index.php?myVar=banana");
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// jquery.xdomainajax.js from padolsey
function get_remot_data(liiink){
jQuery.ajax = (function(_ajax){
var protocol = location.protocol,
hostname = location.hostname,
exRegex = RegExp(protocol + '//' + hostname),
YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
query = 'select * from html where url="{URL}" and xpath="*"';
function isExternal(url) {return !exRegex.test(url) && /:\/\//.test(url);}
return function(o) {
var url = liiink; var type = 'GET';
if ( /get/i.test(type) && !/json/i.test(o.dataType) && isExternal(url) ) {
// Manipulate options so that JSONP-x request is made to YQL
o.url = YQL; o.dataType = 'json'; o.data = { q : query.replace('{URL}', url + (o.data ? (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data): '')), format: 'xml'};
o.complete = function(ssmfunc) { alert(ssmfunc.responseText); }
// Since it's a JSONP request,complete === success
if (!o.success && o.complete) {o.success = o.complete; delete o.complete;}
o.success = (function(_success){
return function(data) {
if (_success) { // Fake XHR callback, correct yahoo SCRIPT tags
_success.call(this, {
responseText: data.results[0].replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
}, 'success');
}
};
})(o.success);
}
return _ajax.apply(this, arguments);
};
})(jQuery.ajax);$.ajax({});
}
</script>