Javascript:document.write(页面)是空白的

时间:2016-08-22 18:47:11

标签: javascript

我需要下面的网站搜索脚本的结果显示在与搜索表单相同的框架/窗口中(包含在下面)。我复制了以下代码:http://www.javascriptsource.com/miscellaneous/site-search.html#comment_listing 并成功获得了win = window.open(“”,“_ self”,“”);在页面=代码后使用我的文本和格式,但搜索结果不会显示。我已经尝试了一个星期来找到解决方案,但没有任何作用,我不明白document.write。

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Dion (biab@iinet.net.au) -->
<!-- Web Site:  http://www.iinet.net.au/~biab -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->

<!-- Begin
var item = new Array();

/* Here is where all the magic happens.  
    Just enter as many additional pages that
    that you want to search, then fill in the
    additional listings for each page.
*/

// "Page Name","path","Page Title","Many,Key,Words","Descriptive Comments"

c=0; item[c]=new Array("index.html","","DemoSite","index,main,start,home,front","Demonstration search engine data about an imagined but probable internet site.");
c++; item[c]=new Array("about.htm","","About Me","about,author,contact,email,who","Contact details and general information about the creator of the site and what the site is about.");
c++; item[c]=new Array("links.htm","","Links page","links,more,where,similar,friends","Links to my favourite sites which I find interesting. Other friends sites which have similar interests to my own.");
c++; item[c]=new Array("main.htm","main/","Main Page","content,main,focus","The main part of my site which contains what you have come to see. Lots of stuff like that and more great things. All in a sub directory.");
c++; item[c]=new Array("logo.jpg","main/images/","Link Logo","link,image,logo,graphic","The logo.jpg is just a small image which you can place on your site as a link to me. It's in a second level subdirectory.");
c++; item[c]=new Array("helphowtocenter.htm","","Help! & How To Center","help,test,computer","lots of help for everyone");
c++; item[c]=new Array("CSR.html","","CSR","help,test,computer","Computer service request form");

page="<html><head><br><title>Search Results</title></head><body bgcolor='white'><p style=font-family:Arial style=font-size:14pt style=color:FF6600><b>Employee-Net Search Results</p></b><HR color=0000FF><left><br><table bgcolor='white' border=0 cellpadding=0 cellspacing=10 width=100%>";


function search(frm) {
win = window.open("","_self","width=800 height=800 scrollbars");
win.document.write(page);
txt = frm.srchval.value.split(" ");
fnd = new Array(); total=0;
for (i = 0; i < item.length; i++) {
fnd[i] = 0; order = new Array(0, 4, 2, 3);
for (j = 0; j < order.length; j++)
for (k = 0; k < txt.length; k++)
if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "")
fnd[i] += (j+1);
}
for (i = 0; i < fnd.length; i++) {
n = 0; w = -1;
for (j = 0;j < fnd.length; j++)
if (fnd[j] > n) { n = fnd[j]; w = j; };
if (w > -1) total += show(w, win, n);
fnd[w] = 0;
}
win.document.write("</table><p style=font-family:Arial style=font-size:10pt>Total found: "+total+"<p></body></html>");
win.document.close();
}
function show(which,wind,num) {
link = item[which][1] + item[which][0]; 
line = "<tr><td style=font-family:Arial style=font-size:12pt><a href='"+link+"'>"+item[which][2]+"</a><br>Description:  ";
line += item[which][4] + "<br>Path:  "+link+"<HR color=C1C1FF style=height:1px></td></tr>";
wind.document.write(line);
return 1;
}
//  End -->
</script>

1 个答案:

答案 0 :(得分:0)

我为你修改了你的源代码,看看它是如何完成的,我使用了jQuery,因为它更容易做到这一点。

我希望它可以帮到你

快乐的代码! :)

<html>
    <head>
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
    </head>
    <script type="text/javascript">
    var item = new Array();
    c=0; item[c]=new Array("index.html","","DemoSite","index,main,start,home,front","Demonstration search engine data about an imagined but probable internet site.");
c++; item[c]=new Array("about.htm","","About Me","about,author,contact,email,who","Contact details and general information about the creator of the site and what the site is about.");
c++; item[c]=new Array("links.htm","","Links page","links,more,where,similar,friends","Links to my favourite sites which I find interesting. Other friends sites which have similar interests to my own.");
c++; item[c]=new Array("main.htm","main/","Main Page","content,main,focus","The main part of my site which contains what you have come to see. Lots of stuff like that and more great things. All in a sub directory.");
c++; item[c]=new Array("logo.jpg","main/images/","Link Logo","link,image,logo,graphic","The logo.jpg is just a small image which you can place on your site as a link to me. It's in a second level subdirectory.");

        function search() {
            var input  = $("#inputSearch").val();
            txt = input.split(" ");
            fnd = new Array(); total=0;
            for (i = 0; i < item.length; i++) {
                fnd[i] = 0; order = new Array(0, 4, 2, 3);
                for (j = 0; j < order.length; j++)
                for (k = 0; k < txt.length; k++)
                if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "")
                fnd[i] += (j+1);
            }
            for (i = 0; i < fnd.length; i++) {
                n = 0; w = -1;
                for (j = 0;j < fnd.length; j++)
                if (fnd[j] > n) { n = fnd[j]; w = j; };
                if (w > -1) total += show(w, n);
                fnd[w] = 0;
            }
        }
        function show(which,num) {
            link = item[which][1] + item[which][0];
            line = "</tr><td><a href='"+link+"'>"+item[which][2]+"</a> Score: "+num+"<br>" + item[which][4] + "<br>"+link+"</td></tr>";
            $("#results").append(line);
}
    </script>
    <body>
        <center>
        <form>
            <tr><td><input type=text name="srchval" id="inputSearch" value=""><button type="button" onclick="search()">Search</button></td></tr>
        </form>
        <div id="container_results">
            <center>
            <table border=0 cellspacing=10 width=80% id="results"></table>
            </center>
        </div>
        </center>
    </body>
</html>