• [ x0429.COM 电 脑 知 识 学 习 网 ]
  • RSS订阅
  • 网站地图
  • 搜索: 您的位置电脑基础知识网>网站素材>阅读资讯文章

    站内搜索脚本(一)

    2008-05-19 10:16:16 来源:网络其他网站 作者:采集 点击: 评论:0
    脚本说明:
    第一步:把如下代码加入%26lt;body%26gt;区域中
    %26lt;script language=%26quot;JavaScript%26quot;%26gt;
    %26lt;!--
    // 下面是负责搜索的页面的全路径或相对路径
    var search_htm_url = %26quot;searchsource.htm%26quot;;

    function searchPage() {
    if ((document.searchpage.keyword.value.length == 0)
    || (document.searchpage.keyword.value == %26quot; %26quot;)) {
    alert(%26quot;请填入关键字!%26quot;);
    } else {
    sel = document.searchpage.and_or.selectedIndex;
    location.href = search_htm_url + %26quot;?%26quot;
    + escape(document.searchpage.keyword.value)
    + (sel==0?%26quot;%26amp;and%26quot;:(sel==2?%26quot;%26amp;exact%26quot;:%26quot;%26amp;or%26quot;));
    }
    return false;
    }

    document.write('%26lt;form name=%26quot;searchpage%26quot; onSubmit=%26quot;return searchPage()%26quot;%26gt;'
    +'请填入关键字: %26lt;input type=%26quot;text%26quot; size=22 name=%26quot;keyword%26quot;%26gt; '
    +'%26lt;input type=%26quot;button%26quot; value=%26quot;Search%26quot; onClick=%26quot;searchPage()%26quot;%26gt;%26lt;br%26gt;'
    +'%26lt;select name=%26quot;and_or%26quot; size=1%26gt;%26lt;option%26gt;关键字与搜索%26lt;option%26gt;关键字或搜索 '
    +'%26lt;option%26gt;关键字异或搜索%26lt;/select%26gt;%26lt;/form%26gt;');
    // --%26gt;
    %26lt;/script%26gt;


    第二步:新建searchsource.htm 其内容为
    %26lt;!-- 代码开始--%26gt;
    %26lt;form NAME=%26quot;database%26quot;%26gt;
    %26lt;input type=%26quot;hidden%26quot; name=%26quot;list%26quot;
    value=%26quot;http://www.jzzy.com~建站资源网网站|njcatv 建站资源网 网页特效 影音空间 新闻 javascript java applet 健康之桥^为广大南京市民提供全方位的信息和帮助*http://www.sina.com.cn~最全的中文新闻发部站点|sina 新浪 新闻 super 王志东 聊天室^国内最大的新闻网站!*http://www.163.net~最大的综合中文网站|163 netease 网易 丁磊 新闻 yeah 126 娱乐 freemail 聊天室^最大的综合中文网站,为你提供全方位的各种信息,是你上网的好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!%26quot;%26gt;
    %26lt;/form%26gt;
    %26lt;!-- 以下代码不要变动--%26gt;
    %26lt;script language=%26quot;JavaScript%26quot;%26gt;
    %26lt;!--
    function Page(url,title,keywords,description) {
    while ((url.length %26gt; 0) %26amp;%26amp; (url.charAt(0) == %26quot; %26quot;)) {
    url = url.substring(1,url.length);
    }
    this.url = url;
    while ((title.length %26gt; 0) %26amp;%26amp; (title.charAt(0) == %26quot; %26quot;)) {
    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(%26quot;~%26quot;,pos)) != -1) {
    pos2 = amorphous.indexOf(%26quot;|%26quot;,pos1+1);
    pos3 = amorphous.indexOf(%26quot;^%26quot;,pos2+1);
    pos4 = amorphous.indexOf(%26quot;*%26quot;,pos3+1);
    if ((pos2 != -1)
    %26amp;%26amp; (pos2 %26lt; pos3) %26amp;%26amp; (pos3 %26lt; pos4)
    %26amp;%26amp; (pos4 %26lt;= amorphous.indexOf(%26quot;*%26quot;,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 %26lt;= amorphous.length)
    alert('Error reading in amorphous database around %26quot;'
    + amorphous.substring(pos,pos+30) + '%26quot;');
    pos = amorphous.indexOf(%26quot;*%26quot;,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(%26quot; %26quot;)) != -1
    %26amp;%26amp; and_search != %26quot;exact%26quot;) {
    words[words_length] = str.substring(0,pos);
    if (words[words_length].length %26gt; 0)
    words_length++;
    if (str.length == 1)
    str=%26quot;%26quot;;
    else
    str = str.substring(pos+1,str.length);
    }
    if (str.length %26gt; 0)
    words[words_length++] = str;
    for (q=0;q%26lt;words_length;q++) {
    temp_length = 0;
    str = words[q].toLowerCase();
    len = (and_search==%26quot;and%26quot;%26amp;%26amp;q%26gt;0?menu_length:database_length);
    for (n=0; n%26lt;len; n++) {
    if (and_search==%26quot;and%26quot;%26amp;%26amp;q%26gt;0) {
    combo = (menu[n].title + %26quot; %26quot; + menu[n].description
    + %26quot; %26quot; + menu[n].keywords).toLowerCase();
    } else {
    combo = (database[n].title + %26quot; %26quot; + database[n].description
    + %26quot; %26quot; + database[n].keywords).toLowerCase();
    }
    if (combo.indexOf(str) != -1) // found
    temp[temp_length++] = (and_search==%26quot;and%26quot;%26amp;%26amp;q%26gt;0?menu[n]:database[n]);
    }
    if (and_search!=%26quot;and%26quot; %26amp;%26amp; q%26gt;0) {
    added = 0;
    for (i=0;i%26lt;temp_length;i++) {
    duplicate = false;
    for (j=0;j%26lt;menu_length%26amp;%26amp;!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%26lt;temp_length;h++)
    menu[h] = temp[h];
    menu_length = temp_length;
    }
    }
    }
    function entry() {
    if ((document.entryform.keyword.value.length == 0)
    || (document.entryform.keyword.value == %26quot; %26quot;)) {
    alert(%26quot;你必须填写关键字!%26quot;);
    return false;
    }
    and_search = (document.entryform.and_or.selectedIndex == 0?%26quot;and%26quot;:%26quot;or%26quot;);
    if (document.entryform.and_or.selectedIndex == 2)
    and_search = %26quot;exact%26quot;;
    location.href = location.pathname + %26quot;?%26quot;
    + escape(document.entryform.keyword.value)
    + (and_search != %26quot;or%26quot;?%26quot;%26amp;%26quot;+and_search:%26quot;%26quot;);
    return false;
    }
    function redWord(str) {
    for(r=0; r%26lt;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) + %26quot;*%26quot;
    + str.substring(pos,val) + %26quot;|%26quot;
    + str.substring(val,str.length);
    }
    }
    pos = -16;
    while ((pos = str.toLowerCase().indexOf(%26quot;*%26quot;,pos+16)) != -1)
    str = str.substring(0,pos) + %26quot;%26lt;font color=red%26gt;%26quot;
    + str.substring(pos+1,str.length);
    pos = -7;
    while ((pos = str.toLowerCase().indexOf(%26quot;|%26quot;,pos+7)) != -1)
    str = str.substring(0,pos) + %26quot;%26lt;/font%26gt;%26quot;
    + 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(%26quot;*%26quot;) == -1)
    amorphous += %26quot;* %26quot;;
    else
    amorphous += %26quot; %26quot;; // 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 = %26quot;%26quot;;
    and_search = %26quot;or%26quot;;
    if (location.search.length %26gt; 1) {
    string = unescape(location.search.substring(1,location.search.length));
    pos = 0;
    while ((pos = string.indexOf('%26quot;',pos)) != -1) {
    string = string.substring(0,pos) + '\%26quot;' + string.substring(pos+1,string.length);
    pos += 2;
    }
    if (string.substring(string.length-4,string.length) == %26quot;%26amp;and%26quot;) {
    string = string.substring(0,string.length-4);
    and_search = %26quot;and%26quot;;
    } else if (string.substring(string.length-6,string.length) == %26quot;%26amp;exact%26quot;) {
    string = string.substring(0,string.length-6);
    and_search = %26quot;exact%26quot;;
    } else if (string.substring(string.length-3,string.length) == %26quot;%26amp;or%26quot;) {
    string = string.substring(0,string.length-3);
    and_search = %26quot;or%26quot;;
    }
    search(string);
    }
    document.write('%26lt;form name=%26quot;entryform%26quot; onSubmit=%26quot;return entry()%26quot;%26gt;'
    +'Search for:%26lt;input type=%26quot;text%26quot; size=22 '
    +'name=%26quot;keyword%26quot; value=%26quot;'+string+'%26quot;%26gt; '
    +'%26lt;input type=%26quot;button%26quot; value=%26quot;Search%26quot; onClick=%26quot;entry()%26quot;%26gt;%26lt;br%26gt;%26lt;select name=%26quot;and_or%26quot; '
    +'size=1%26gt;%26lt;option'+(and_search==%26quot;and%26quot;?%26quot; selected%26quot;:%26quot;%26quot;)+'%26gt;Search All word '
    +'(AND)%26lt;option'+(and_search==%26quot;or%26quot;?%26quot; selected%26quot;:%26quot;%26quot;)+'%26gt;Search any word '
    +'(OR)%26lt;option'+(and_search==%26quot;exact%26quot;?%26quot; selected%26quot;:%26quot;%26quot;)+'%26gt;Exact '
    +'word%26lt;/select%26gt;%26lt;/form%26gt;%26lt;br%26gt;');
    if (location.search.length %26gt; 1)
    document.write('查询结果:%26lt;br%26gt;%26lt;br%26gt;n');

    for (n=0; n%26lt;menu_length; n++)
    document.write('%26lt;a href=%26quot;'+menu[n].url+'%26quot;%26gt;'+menu[n].title
    +'%26lt;/a%26gt;%26lt;br%26gt;'+redWord(menu[n].description)+'%26lt;br%26gt;Keywords: '
    +redWord(menu[n].keywords)+'%26lt;br%26gt;%26lt;br%26gt;n');
    if ((menu_length == 0) %26amp;%26amp; (location.search.length %26gt; 1))
    document.write('对不起:你查询的关键字%26quot;'+string+'%26quot;没有发现!n');
    // --%26gt;
    %26lt;/script%26gt;



    Tags:[标签:标签]  
    责任编辑:
  • 请文明参与讨论,禁止漫骂攻击。 用户名:新注册) 密码: 匿名:
    评论总数:0 [ 查看全部 ] 网友评论