Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created August 5, 2010 03:52
Show Gist options
  • Save rutcreate/509202 to your computer and use it in GitHub Desktop.
Save rutcreate/509202 to your computer and use it in GitHub Desktop.
JavaScript Learning with P'Pok
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
(function($) {
$.fn.extend({
scanLink: function(options) {
var defaults = {};
var options = $.extend(defaults, options);
var replace = function(str, word, link) {
var reg = new RegExp(word, 'gi');
var replace_text = '<a href="'+link+'">'+word+'</a>';
return str.replace(reg, replace_text);
};
return this.each(function() {
var content = $(this);
if (options.map) {
$.each(options.map, function(word, link) {
var replaced = replace(content.html(), word, link);
content.html(replaced);
});
}
});
}
});
})(jQuery);
$(document).ready(function() {
// Create map object.
var map = {};
$('#mapping tr').each(function(i, row) {
var columns = $('td', row);
map[columns.eq(0).text()] = columns.eq(1).text();
});
// Scan link.
$('.news').scanLink({'map': map});
});
</script>
</head>
<body>
<table id="mapping">
<tr>
<td>bangkok</td>
<td>http://wikipedia.org/wiki/Thailand</td>
</tr>
<tr>
<td>Pakistan</td>
<td>http://wikipedia.org/wiki/Pakistan</td>
</tr>
<tr>
<td>Britain</td>
<td>http://wikipedia.org/wiki/England</td>
</tr>
<tr>
<td>India</td>
<td>http://wikipedia.org/wiki/India</td>
</tr>
<tr>
<td>London</td>
<td>http://wikipedia.org/wiki/London</td>
</tr>
</table>
<div class="news">
Pakistan attempted to calm its diplomatic fallout with Britain by confirming that President Zardari would go ahead with a planned trip to meet David Cameron, despite anger over the Prime Minister’s comments that the country was “exporting terror”. Mr Zardari was under domestic pressure to cancel the trip after fury over Mr Cameron’s comments - made during last week’s visit to India - which have already led Pakistan’s powerful Inter-Services Intelligence (ISI) agency to cancel talks with British security officials in London. Opposition politicians in Pakistan had urged Mr Zardari to call off his trip, while demonstrators burnt an effigy of the Prime Minister on the streets of Karachi
</div>
</body>
</html>
@pphetra
Copy link

pphetra commented Aug 5, 2010

บรรทัด 17-19 ตั้งชื่อ o, obj คล้ายกันไปหน่อย คนอ่านโปรแกรมที่หลัง(programmer ผู้โชคดี ที่ต้องแก้ code นี้) ต้องออกแรงแยกแยะเยอะไป

@rutcreate
Copy link
Author

แก้เรียบร้อยครับ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment