Javascript搜索引擎(搜索自己的网站) [英] Javascript Search Engine (Search own site)

查看:267
本文介绍了Javascript搜索引擎(搜索自己的网站)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个只搜索我自己的网站的搜索引擎。我目前有一些JavaScript,但它只在特定页面上搜索单词。如果可能的话,我需要它搜索我网站中的链接。

我的网站位于内部Intranet上,因此无法使用Google搜索引擎。

 < SCRIPT language = JavaScript> 
var NS4 =(document.layers);
var IE4 =(document.all);
var win = window;
var n = 0;
函数findInPage(str){
var txt,i,found;
if(str ==)
return false;
如果(NS4){
if(!win.find(str))
while(win.find(str,false,true))
n ++;
else
n ++;
if(n == 0)
alert(Not found。);
}
if(IE4){
txt = win.document.body.createTextRange();
for(i = 0; i< = n&&;(found = txt.findText(str))!= false; i ++){
txt.moveStart(character,1) ;
txt.moveEnd(textedit);
}
if(found){
txt.moveStart(character,-1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n ++;
}
else {
if(n> 0){
n = 0;
findInPage(str);
}
else
alert(对不起,我们找不到了。再试一次);
}
}
返回false;
}
< / SCRIPT>




(onsubmit =return findInPage(this.string.value); )


它可以很好地搜索那个页面,但我希望有一种方法可以搜索所有页面我的网站。

解决方案

几点建议:


  1. 除非必须,否则不要重新发明轮子 - 有开源库,如Tipue Search( Tipue Search )和其他。

  2. 您可以使用jquery / ajax $ .load()来动态加载页面内容并搜索它们,同时仍然保留在相同的页面中,直到您的DOM和脚本

  3. NodeJS也是一个不错的选择,但可能会被杀死。

希望这有助于!


I want to have a search engine which searches only my own site. I have some JavaScript currently, but it only searches words on that specific page. I need it to search the links within my site if possible.

I cannot use the Google search engine as my site is on an internal intranet.

<SCRIPT language=JavaScript>
var NS4 = (document.layers);    
var IE4 = (document.all);
var win = window;    
var n   = 0;
function findInPage(str) {
var txt, i, found;
if (str == "")
return false;
if (NS4) {
if (!win.find(str))
  while(win.find(str, false, true))
    n++;
else
  n++;
if (n == 0)
  alert("Not found.");
}
if (IE4) {
txt = win.document.body.createTextRange();
for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
  txt.moveStart("character", 1);
  txt.moveEnd("textedit");
}
if (found) {
  txt.moveStart("character", -1);
  txt.findText(str);
  txt.select();
  txt.scrollIntoView();
  n++;
}
else {
  if (n > 0) {
    n = 0;
    findInPage(str);
  }
  else
    alert("Sorry, we couldn't find.Try again");
}
}
return false;
}
</SCRIPT>

(onsubmit="return findInPage(this.string.value); in the button tag.)

It works great for searching that page, but I was hoping there was a way to search all pages on my site.

解决方案

Few suggestions:

  1. Unless you must, don't re-invent the wheel - there are open source libraries such as Tipue Search (Tipue Search) and others.
  2. You can use jquery/ajax $.load() to dynamically load page content and search them, while still staying in the same page as far as your DOM and script goes.
  3. NodeJS is also a good option, but will probably be an over kill.

Hope this helps!

这篇关于Javascript搜索引擎(搜索自己的网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆