/ search

Javascript搜索文本

某项目中用到的Javascript搜索文本,源于网络。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>单位查询</title>
    <style type="text/css">
        body {
            font-size: 12px;
            line-height: 1.5;
            color: #444;
            padding: 8px;
            margin: 0;
        }

        #sresult {
            width: 700px;
            padding: 5px;
            color: #deb887;
        }

        #sresult a {
            color: #a52a2a;
            margin: 0 5px;
        }

        .tips {
            font-weight: bold;
            color: green;
        }

    </style>
</head>
<body>

<form name="database">
    <input type='hidden' name='list'
           value='旅游局~旅游局|旅游局^旅游局*苹果园街道~苹果园街道|苹果园街道^苹果园街道*气象局~气象局|气象局^气象局*侨联~侨联|侨联^侨联*区工会~区工会|区工会^区工会*区人大办~区人大办|区人大办^区人大办*区委办~区委办|区委办^区委办*区委党校~区委党校|区委党校^区委党校*区研究室~区研究室|区研究室^区研究室*区政协办~区政协办|区政协办^区政协办*区志办~区志办|区志办^区志办*人力社保局~人力社保局|人力社保局^人力社保局*商务委~商务委|商务委^商务委*社会工委~社会工委|社会工委^社会工委*审计局~审计局|审计局^审计局*石景山医院~石景山医院|石景山医院^石景山医院*实兴腾飞公司~实兴腾飞公司|实兴腾飞公司^实兴腾飞公司*市场管理中心~市场管理中心|市场管理中心^市场管理中心*市政市容委~市政市容委|市政市容委^市政市容委*司法局~司法局|司法局^司法局*体育局~体育局|体育局^体育局*统计局~统计局|统计局^统计局*统战部~统战部|统战部^统战部*投促局~投促局|投促局^投促局*团委~团委|团委^团委*外事办~外事办|外事办^外事办*万商投资公司~万商投资公司|万商投资公司^万商投资公司*卫生局~卫生局|卫生局^卫生局*文联~文联|文联^文联*文明办~文明办|文明办^文明办*文委~文委|文委^文委*武装部~武装部|武装部^武装部*五里坨街道~五里坨街道|五里坨街道^五里坨街道*西建办~西建办|西建办^西建办*行政服务中心~行政服务中心|行政服务中心^行政服务中心*宣传部(文创办)~宣传部(文创办)|宣传部(文创办)^宣传部(文创办)*烟草专卖局~烟草专卖局|烟草专卖局^烟草专卖局*燕金源公司~燕金源公司|燕金源公司^燕金源公司*药监局~药监局|药监局^药监局*游乐园~游乐园|游乐园^游乐园*园林局~园林局|园林局^园林局*政法委~政法委|政法委^政法委*政府办~政府办|政府办^政府办*质监局~质监局|质监局^质监局*住建委~住建委|住建委^住建委*综治办~综治办|综治办^综治办*组织部~组织部|组织部^组织部*'>
</form>
<script type="text/javascript">
<!--
//选中单位
function selYjdw(selyjdw) {
    window.parent.document.getElementById('rdyjdw').value = selyjdw;
    alert('单位已经设置为:' + selyjdw + '!');
    return false;
}

function Page(url, title, keywords, description) {
    while ((url.length > 0) && (url.charAt(0) == " ")) {
        url = url.substring(1, url.length);
    }
    this.url = url;
    while ((title.length > 0) && (title.charAt(0) == " ")) {
        title = title.substring(1, title.length);
    }
    this.title = title;
    this.keywords = keywords;
    this.description = description;
    return this;
}
function Database() {
    var pos = 0;
    while ((pos1 = amorphous.indexOf("~", pos)) != -1) {
        pos2 = amorphous.indexOf("|", pos1 + 1);
        pos3 = amorphous.indexOf("^", pos2 + 1);
        pos4 = amorphous.indexOf("*", pos3 + 1);
        if ((pos2 != -1) && (pos2 < pos3) && (pos3 < pos4) && (pos4 <= amorphous.indexOf("*", pos))) {
            this [database_length++] = new Page(amorphous.substring(pos, pos1), amorphous.substring(pos1 + 1,
                    pos2), amorphous.substring(pos2 + 1, pos3), amorphous.substring(pos3 + 1, pos4));
            pos = pos4 + 1;
        }
        else {
            // error reading amorphous database
            if (pos + 30 <= amorphous.length) {
                alert('Error reading in amorphous database around "' + amorphous.substring(pos, pos + 30) + '"');
            }
            pos = amorphous.indexOf("*", pos) + 1;
        }
    }
    return this;
}
function search(str) {
    menu_length = 0;
    temp = new Object();
    temp_length = 0;
    words_length = 0;
    words = new Object();
    pos = 0;
    while ((pos = str.indexOf(" ")) != -1 && and_search != "exact") {
        words[words_length] = str.substring(0, pos);
        if (words[words_length].length > 0) {
            words_length++;
        }
        if (str.length == 1) {
            str = "";
        }
        else {
            str = str.substring(pos + 1, str.length);
        }
    }
    if (str.length > 0) {
        words[words_length++] = str;
    }
    for (q = 0; q < words_length; q++) {
        temp_length = 0;
        str = words[q].toLowerCase();
        len = (and_search == "and" && q > 0 ? menu_length : database_length);
        for (n = 0; n < len; n++) {
            if (and_search == "and" && q > 0) {
                combo = (menu[n].title + " " + menu[n].description + " " + menu[n].keywords).toLowerCase();
            }
            else {
                combo = (database[n].title + " " + database[n].description + " " + database[n].keywords).toLowerCase();
            }
            if (combo.indexOf(str) != -1) {
                // found
                temp[temp_length++] = (and_search == "and" && q > 0 ? menu[n] : database[n]);
            }
        }
        if (and_search != "and" && q > 0) {
            added = 0;
            for (i = 0; i < temp_length; i++) {
                duplicate = false;
                for (j = 0; j < menu_length && !duplicate; j++) {
                    if (menu[j] == temp[i]) {
                        duplicate = true;
                    }
                }
                if (!duplicate) {
                    menu[menu_length + (added++)] = temp[i];
                }
            }
            menu_length += added;
        }
        else {
            for (h = 0; h < temp_length; h++) {
                menu[h] = temp[h];
            }
            menu_length = temp_length;
        }
    }
}
function entry() {
    if ((document.entryform.keyword.value.length == 0) || (document.entryform.keyword.value == " ")) {
        alert("必须填写关键字!");
        return false;
    }
    and_search = (document.entryform.and_or.selectedIndex == 0 ? "and" : "or");
    if (document.entryform.and_or.selectedIndex == 2) {
        and_search = "exact";
    }
    location.href = location.pathname + "?" + escape(document.entryform.keyword.value) + (and_search != "or" ? "&" + and_search : "");
    return false;
}
function redWord(str) {
    for (r = 0; r < words_length; r++) {
        pos = -3;
        word = words[r].toLowerCase();
        while ((pos = str.toLowerCase().indexOf(word, pos + 3)) != -1) {
            val = pos + word.length;
            str = str.substring(0, pos) + "*" + str.substring(pos, val) + "|" + str.substring(val, str.length);
        }
    }
    pos = -16;
    while ((pos = str.toLowerCase().indexOf("*", pos + 16)) != -1) {
        str = str.substring(0, pos) + "<font color=red>" + str.substring(pos + 1, str.length);
    }
    pos = -7;
    while ((pos = str.toLowerCase().indexOf("|", pos + 7)) != -1) {
        str = str.substring(0, pos) + "</font>" + str.substring(pos + 1, str.length);
    }
    return str;
}
var amorphous = document.database.list.value;
temp_str = amorphous.substring(amorphous.length - 2, amorphous.length);
if (temp_str.indexOf("*") == -1) {
    amorphous += "* ";
}
else {
    amorphous += " ";
}
// amorphous database must have characters after last asterisk
database_length = 0;
// Netscape 2 fix
var database = new Database();
// read in from amorphous database
menu_length = 0;
// Netscape 2 fix
var menu = new Object();
string = "";
and_search = "or";
if (location.search.length > 1) {
    string = unescape(location.search.substring(1, location.search.length));
    pos = 0;
    while ((pos = string.indexOf('"', pos)) != -1) {
        string = string.substring(0, pos) + '\\"' + string.substring(pos + 1, string.length);
        pos += 2;
    }
    if (string.substring(string.length - 4, string.length) == "&and") {
        string = string.substring(0, string.length - 4);
        and_search = "and";
    }
    else if (string.substring(string.length - 6, string.length) == "&exact") {
        string = string.substring(0, string.length - 6);
        and_search = "exact";
    }
    else if (string.substring(string.length - 3, string.length) == "&or") {
        string = string.substring(0, string.length - 3);
        and_search = "or";
    }
    search(string);
}

document.write('<form name="entryform" onSubmit="return entry()">'
        + '单位较多,可在右侧进行搜索: <input type="text" size=22 '
        + 'name="keyword" value="' + string + '"> '
        + '<input type="button" value="搜索" onClick="entry()"><br><select style="display:none" name="and_or" '
        + 'size=1><option' + (and_search == "and" ? " selected" : "") + '>Search All word '
        + '(AND)<option' + (and_search == "or" ? " selected" : "") + '>Search any word '
        + '(OR)<option' + (and_search == "exact" ? " selected" : "") + '>Exact '
        + 'word</select></form>');

if (location.search.length > 1) {
    document.write('<span style="color:#777">查询结果:</span> <span class="tips">!点击下面的链接即可选中单位!</span>\n<div id="sresult">');
    for (n = 0; n < menu_length; n++) {
        document.write('<a href="#" onclick="selYjdw(\'' + menu[n].url + '\')">' + redWord(menu[n].description) + '</a> | \n');
    }
    document.write('</div>');
}

if ((menu_length == 0) && (location.search.length > 1)) {
    document.write('<div id="sresult">未找到与关键字[ <strong style="color:red">' + string + '</strong> ]匹配的条目!</div>\n');
}

// -->
</script>

</body>
</html>