├── README ├── ajaxcontent.html ├── waiting-for-godot.php ├── simple-ajax.js ├── reusable-ajax.js ├── using-proxy.js ├── allowing-link-following.js ├── code.js ├── styles.css ├── proxy.php ├── simple-ajax.html ├── using-proxy.html ├── using-yql.html ├── reusable-ajax.html ├── using-yql.js ├── allowing-link-following.html ├── error-handling.html ├── index.html ├── error-handling.js └── load-external.js /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ajaxcontent.html: -------------------------------------------------------------------------------- 1 |

This is some demo content requested by Ajax. I am an awesome coder, me.

-------------------------------------------------------------------------------- /waiting-for-godot.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /simple-ajax.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | $('.ajaxtrigger').click(function(){ 3 | $('#target').load('ajaxcontent.html'); 4 | }); 5 | }); -------------------------------------------------------------------------------- /reusable-ajax.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | $('.ajaxtrigger').click(function(){ 3 | $('#target').load($(this).attr('href')); 4 | return false; 5 | }); 6 | }); -------------------------------------------------------------------------------- /using-proxy.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | $('.ajaxtrigger').click(function(){ 3 | var url = $(this).attr('href'); 4 | if(url.match('^http')){ 5 | url = 'proxy.php?url=' + url; 6 | } 7 | $('#target').load(url); 8 | return false; 9 | }); 10 | }); -------------------------------------------------------------------------------- /allowing-link-following.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | $('.ajaxtrigger').click(function(){ 3 | var url = $(this).attr('href'); 4 | if(url.match('^http')){ 5 | return true; 6 | } else { 7 | $('#target').load(url); 8 | return false; 9 | } 10 | }); 11 | }); -------------------------------------------------------------------------------- /code.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | var url = $('script').last().attr('src'); 3 | $.get(url, 4 | function(code){ 5 | console.log(code); 6 | code=code.replace(/&/mg,'&'); 7 | code=code.replace(//mg,'>'); 9 | code=code.replace(/\"/mg,'"'); 10 | code=code.replace(/\t/g,' '); 11 | code=code.replace(/\r?\n/g,'
'); 12 | code=code.replace(/

/g,'
'); 13 | code=code.replace(/ /g,' '); 14 | $('#code').html('
'+code+'
'); 15 | } 16 | ); 17 | }); -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | *{ 2 | margin:0; 3 | padding:0; 4 | } 5 | body{border:2em solid #fff;font-family:futura,verdana,sans-serif;} 6 | a{color:#369} 7 | ul{margin:1em} 8 | h1{font-size:25px;color:#036;} 9 | h2{font-size:18px;margin:10px 0;color:#69c;} 10 | #target,#code{ 11 | border:1px solid #999; 12 | background:#eee; 13 | padding:1em; 14 | -moz-border-radius:5px; 15 | border-radius:5px; 16 | -webkit-border-radius:5px; 17 | } 18 | #target{ 19 | overflow:auto; 20 | max-height:200px; 21 | } 22 | #ft{font-size:12px;text-align:right;} 23 | .ajaxtrigger span{font-weight:bold;color:#000} 24 | .ajaxtrigger span.error{color:#c00} 25 | -------------------------------------------------------------------------------- /proxy.php: -------------------------------------------------------------------------------- 1 | ]*>/msi','',$output); 14 | $content = preg_replace('/<\/body>.*/msi','',$content); 15 | $content = preg_replace('/]*>/msi','',$content); 16 | $content = preg_replace('/[\r|\n]+/msi','',$content); 17 | $content = preg_replace('/<--[\S\s]*?-->/msi','',$content); 18 | $content = preg_replace('/]*>[\S\s]*?<\/noscript>/msi', 19 | '',$content); 20 | $content = preg_replace('/]*>[\S\s]*?<\/script>/msi', 21 | '',$content); 22 | $content = preg_replace('//msi','',$content); 23 | echo $content; 24 | } else { 25 | echo 'Error: URL not allowed to load here.'; 26 | } 27 | ?> -------------------------------------------------------------------------------- /simple-ajax.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - obtrusive and bad 7 | 8 | 9 | 10 |
11 | 12 |
13 |

Demo

14 | 17 |
18 |

Source Code

19 |
20 | 21 |
22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /using-proxy.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - using a proxy 7 | 8 | 9 | 10 |
11 | 12 |
13 |

Demo

14 | 18 |
19 |

Source Code

20 |
21 | 22 |
23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /using-yql.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - using YQL 7 | 8 | 9 | 10 |
11 | 12 |
13 |

Demo

14 | 18 |
19 |

Source Code

20 |
21 | 22 |
23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /reusable-ajax.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - less obtrusive 7 | 8 | 9 | 10 |
11 | 12 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /using-yql.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | var container = $('#target'); 3 | $('.ajaxtrigger').click(function(){ 4 | doAjax($(this).attr('href')); 5 | return false; 6 | }); 7 | function doAjax(url){ 8 | if(url.match('^http')){ 9 | $.getJSON("http://query.yahooapis.com/v1/public/yql?"+ 10 | "q=select%20*%20from%20html%20where%20url%3D%22"+ 11 | encodeURIComponent(url)+ 12 | "%22&format=xml'&callback=?", 13 | function(data){ 14 | if(data.results[0]){ 15 | var data = filterData(data.results[0]); 16 | container.html(data); 17 | } else { 18 | var errormsg = '

Error: could not load the page.

'; 19 | container.html(errormsg); 20 | } 21 | } 22 | ); 23 | } else { 24 | $('#target').load(url); 25 | } 26 | } 27 | function filterData(data){ 28 | data = data.replace(/]*>/g,''); 29 | data = data.replace(/[\r|\n]+/g,''); 30 | data = data.replace(/<--[\S\s]*?-->/g,''); 31 | data = data.replace(/]*>[\S\s]*?<\/noscript>/g,''); 32 | data = data.replace(/]*>[\S\s]*?<\/script>/g,''); 33 | data = data.replace(//,''); 34 | return data; 35 | } 36 | }); 37 | 38 | 39 | -------------------------------------------------------------------------------- /allowing-link-following.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - falling back to link following 7 | 8 | 9 | 10 |
11 | 12 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /error-handling.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Ajax with jQuery - adding error handling 7 | 8 | 9 | 10 |
11 | 12 |
13 |

Demo

14 | 21 |
22 |

Source Code

23 |
24 | 25 |
26 | 27 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Loading external content with Ajax using jQuery and YQL 7 | 8 | 9 | 10 |
11 | 12 | 26 | 27 |
28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /error-handling.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | var container = $('#target'); 3 | container.attr('tabIndex','-1'); 4 | $('.ajaxtrigger').click(function(){ 5 | var trigger = $(this); 6 | var url = trigger.attr('href'); 7 | if(!trigger.hasClass('loaded')){ 8 | trigger.append(''); 9 | trigger.addClass('loaded'); 10 | var msg = trigger.find('span').last(); 11 | } else { 12 | var msg = trigger.find('span').last(); 13 | } 14 | doAjax(url,msg,container); 15 | return false; 16 | }); 17 | 18 | function doAjax(url,msg,container){ 19 | // if the URL starts with http 20 | if(url.match('^http')){ 21 | // assemble the YQL call 22 | msg.removeClass('error'); 23 | msg.html(' (loading...)'); 24 | $.getJSON("http://query.yahooapis.com/v1/public/yql?"+ 25 | "q=select%20*%20from%20html%20where%20url%3D%22"+ 26 | encodeURIComponent(url)+ 27 | "%22&format=xml'&callback=?", 28 | function(data){ 29 | if(data.results[0]){ 30 | var data = filterData(data.results[0]); 31 | msg.html(' (ready.)'); 32 | container. 33 | html(data). 34 | focus(). 35 | effect("highlight",{},1000); 36 | } else { 37 | msg.html(' (error!)'); 38 | msg.addClass('error'); 39 | var errormsg = '

Error: could not load the page.

'; 40 | container. 41 | html(errormsg). 42 | focus(). 43 | effect('highlight',{color:'#c00'},1000); 44 | } 45 | } 46 | ); 47 | } else { 48 | $.ajax({ 49 | url: url, 50 | timeout:5000, 51 | success: function(data){ 52 | msg.html(' (ready.)'); 53 | container. 54 | html(data). 55 | focus(). 56 | effect("highlight",{},1000); 57 | }, 58 | error: function(req,error){ 59 | msg.html(' (error!)'); 60 | msg.addClass('error'); 61 | if(error === 'error'){error = req.statusText;} 62 | var errormsg = 'There was a communication error: '+error; 63 | container. 64 | html(errormsg). 65 | focus(). 66 | effect('highlight',{color:'#c00'},1000); 67 | }, 68 | beforeSend: function(data){ 69 | msg.removeClass('error'); 70 | msg.html(' (loading...)'); 71 | } 72 | }); 73 | } 74 | } 75 | function filterData(data){ 76 | // filter all the nasties out 77 | // no body tags 78 | data = data.replace(/]*>/g,''); 79 | // no linebreaks 80 | data = data.replace(/[\r|\n]+/g,''); 81 | // no comments 82 | data = data.replace(/<--[\S\s]*?-->/g,''); 83 | // no noscript blocks 84 | data = data.replace(/]*>[\S\s]*?<\/noscript>/g,''); 85 | // no script blocks 86 | data = data.replace(/]*>[\S\s]*?<\/script>/g,''); 87 | // no self closing scripts 88 | data = data.replace(//,''); 89 | // [... add as needed ...] 90 | return data; 91 | } 92 | }); 93 | 94 | 95 | -------------------------------------------------------------------------------- /load-external.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | var container = $('#target'); 3 | container.attr('tabIndex','-1'); 4 | $('.ajaxtrigger').click(function(){ 5 | var trigger = $(this); 6 | var url = trigger.attr('href'); 7 | if(!trigger.hasClass('loaded')){ 8 | trigger.append(''); 9 | trigger.addClass('loaded'); 10 | var msg = trigger.find('span').last(); 11 | } else { 12 | var msg = trigger.find('span').last(); 13 | } 14 | doAjax(url,msg,container); 15 | return false; 16 | }); 17 | 18 | function doAjax(url,msg,container){ 19 | // if the URL starts with http 20 | if(url.match('^http')){ 21 | // assemble the YQL call 22 | msg.removeClass('error'); 23 | msg.html(' (loading...)'); 24 | $.getJSON("http://query.yahooapis.com/v1/public/yql?"+ 25 | "q=select%20*%20from%20html%20where%20url%3D%22"+ 26 | encodeURIComponent(url)+ 27 | "%22&format=xml'&callback=?", 28 | function(data){ 29 | if(data.results[0]){ 30 | var data = filterData(data.results[0]); 31 | msg.html(' (ready.)'); 32 | container. 33 | html(data). 34 | focus(). 35 | effect("highlight",{},1000); 36 | } else { 37 | msg.html(' (error!)'); 38 | msg.addClass('error'); 39 | var errormsg = '

Error: could not load the page.

'; 40 | container. 41 | html(errormsg). 42 | focus(). 43 | effect('highlight',{color:'#c00'},1000); 44 | } 45 | } 46 | ); 47 | } else { 48 | $.ajax({ 49 | url: url, 50 | timeout:5000, 51 | success: function(data){ 52 | msg.html(' (ready.)'); 53 | container. 54 | html(data). 55 | focus(). 56 | effect("highlight",{},1000); 57 | }, 58 | error: function(req,error){ 59 | msg.html(' (error!)'); 60 | msg.addClass('error'); 61 | if(error === 'error'){error = req.statusText;} 62 | var errormsg = 'There was a communication error: '+error; 63 | container. 64 | html(errormsg). 65 | focus(). 66 | effect('highlight',{color:'#c00'},1000); 67 | }, 68 | beforeSend: function(data){ 69 | msg.removeClass('error'); 70 | msg.html(' (loading...)'); 71 | } 72 | }); 73 | } 74 | } 75 | function filterData(data){ 76 | // filter all the nasties out 77 | // no body tags 78 | data = data.replace(/]*>/g,''); 79 | // no linebreaks 80 | data = data.replace(/[\r|\n]+/g,''); 81 | // no comments 82 | data = data.replace(/<--[\S\s]*?-->/g,''); 83 | // no noscript blocks 84 | data = data.replace(/]*>[\S\s]*?<\/noscript>/g,''); 85 | // no script blocks 86 | data = data.replace(/]*>[\S\s]*?<\/script>/g,''); 87 | // no self closing scripts 88 | data = data.replace(//,''); 89 | // [... add as needed ...] 90 | return data; 91 | } 92 | }); 93 | 94 | 95 | --------------------------------------------------------------------------------