简体   繁体   中英

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 !!)".

As far as my knowledge for the security reason we cant use java script to access file system in client side. However here it is server side, can that be done?

You're correct. JavaScript cannot access the file system under any circumstances. This would be a HUGE security risk because JS runs, in may cases, without the users consent.

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.

AJAX Resources

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

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. An AJAX call is pure Javascript.

This can easily be done with jQuery's .load() function. 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.

Documentation here:

http://api.jquery.com/load/
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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