1

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

8 Comments

Not exactly true to that extreme. Javascript has a number of filesystem functions that will work when you run it locally.
@evan, The OP is not talking about running the JS locally. Also, can you provide some examples of these file system functions that JS has?
Please have a look into this example, Is this can be modified to get my requirement ..
@Jithu, the example you posted is an example of accessing a file on the server using AJAX (Asynchronous Javascript and XML). This is the method I mentioned in my answer. So, yes, this method can be adapted to suit your needs. I've edited my post to point you in the right direction if you're new to using AJAX.
@James - sorry, only saw the top portion - which accesses the file directly on the local system. Didn't see the answers which do use AJAX.
|
2

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!' );
     };

});

1 Comment

Thank you,TS for your detailed explanation :)
1

You can't do it directly. You CAN use javascript in an AJAX call to trigger a server side script to do your checks.

3 Comments

Please have a look into this example
The example you send is allowing you to open a file locally. It only works when you run the javascript directly on the computer you are accessing. It won't work for your scenario.
Thank you evan, let me try with Ajax !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.