简体   繁体   English

如何使用javascript搜索服务器端文件中的特定文本?

[英]How to search a particular text in a serverside file, using javascript?

As far as I think the answer should be no for this question, how ever would like to check if we have any work around. 据我认为,这个问题的答案应该是“否”,请问如何检查我们是否有任何解决方法。

"I have a client side search box (Text box) and can I check for the text, that the user entered inside the textbox, in the files (HTML) reside in server side (Lets be specific - inside a particular folder I have 5 HTML files), using javascript (Without using any server side coding !!)". “我有一个客户端搜索框(文本框),我可以检查用户在文本框内输入的文本,文件(HTML)位于服务器端(具体来说-在特定文件夹内,我有5个HTML文件),使用javascript(不使用任何服务器端编码!!)”。

As far as my knowledge for the security reason we cant use java script to access file system in client side. 据我所知,出于安全原因,我们无法使用Java脚本访问客户端中的文件系统。 However here it is server side, can that be done? 但是这里是服务器端,可以吗?

You're correct. 没错 JavaScript cannot access the file system under any circumstances. JavaScript在任何情况下都无法访问文件系统。 This would be a HUGE security risk because JS runs, in may cases, without the users consent. 这将是巨大的安全风险,因为在某些情况下,如果未经用户同意,JS会运行。

If you would like to fetch data from a file on the server with JavaScript, have your JS use AJAX to ask the server for information. 如果您想使用JavaScript从服务器上的文件中获取数据,请让JS使用AJAX向服务器询问信息。

AJAX Resources AJAX资源

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

http://www.w3schools.com/ajax/ajax_intro.asp http://www.w3schools.com/ajax/ajax_intro.asp

Yes, as long as the files have public URIs (meaning you can access them in your browser by typing http://example.com/yourfile ), you can most certainly access them directly, with only Javascript, using no server code. 是的,只要文件具有公共URI(这意味着您可以通过输入http://example.com/yourfile在浏览器中访问它们),则可以肯定,您可以直接使用Javascript直接访问它们,而无需使用服务器代码。 An AJAX call is pure Javascript. AJAX调用是纯Javascript。

This can easily be done with jQuery's .load() function. 这可以通过jQuery的.load()函数轻松完成。 This will get the file, or even a part of it, and put it into an element for you to work with. 这将获取文件甚至文件的一部分,并将其放入一个元素中供您使用。 You can also use .get() and work with the text directly. 您也可以使用.get()并直接处理文本。

Documentation here: 此处的文档:

http://api.jquery.com/load/ http://api.jquery.com/load/
http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

Sample: 样品:

$.get( 'http://example.com/yourfile.html', function( data ) {

    if( data.indexOf( 'your search text' ) > -1 ) {
         alert( 'search text found!' );
     };

});

You can't do it directly. 您不能直接这样做。 You CAN use javascript in an AJAX call to trigger a server side script to do your checks. 您可以在AJAX调用中使用javascript来触发服务器端脚本进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM