Mercurial > hg > LGSearch
comparison search.js @ 7:23dcd1b5e9c4 default tip
add keywords highlighting on searching result
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 16 Nov 2015 16:58:39 +0100 |
parents | 4b9ae7d500f9 |
children |
comparison
equal
deleted
inserted
replaced
6:4b9ae7d500f9 | 7:23dcd1b5e9c4 |
---|---|
41 } else { | 41 } else { |
42 return true; | 42 return true; |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 | |
46 function checkPassword() { | 47 function checkPassword() { |
47 var password = document.getElementById("pass").value; | 48 var password = document.getElementById("pass").value; |
48 $.ajax({ | 49 $.ajax({ |
49 url : 'search_function.php', | 50 url : 'search_function.php', |
50 async : false, | 51 async : false, |
79 function closePopup() { | 80 function closePopup() { |
80 if (document.getElementById("pass")) { | 81 if (document.getElementById("pass")) { |
81 document.getElementById("popup").style.display = "none"; | 82 document.getElementById("popup").style.display = "none"; |
82 } | 83 } |
83 } | 84 } |
85 | |
86 | |
87 $(document).ready(function(){ | |
88 highlightKeywords(); | |
89 }) | |
90 | |
91 function highlightKeywords() // highlight keywords in content column, with class="content" | |
92 { | |
93 if(!$("#keywords")[0]) | |
94 return; | |
95 | |
96 var keywords = $("#keywords")[0].innerHTML; | |
97 var keywordsArray = keywords.split(", "); | |
98 //console.log("keywordsArray: "+keywordsArray); | |
99 | |
100 var content = $(".content"); | |
101 for (var i = 0; i < content.length; i++) { | |
102 // find keywords in content[i] | |
103 var text = content[i].innerHTML; | |
104 for (var j = 0; j < keywordsArray.length; j++) { | |
105 var index = text.indexOf(keywordsArray[j]); | |
106 if (index >= 0) { | |
107 text = text.substring(0,index) + "<span class='highlight'>" + text.substring(index, index+keywordsArray[j].length) + "</span>" + text.substring(index+keywordsArray[j].length); | |
108 content[i].innerHTML = text; | |
109 } | |
110 }; | |
111 | |
112 }; | |
113 } |