jQuery Read Text File

Today, I am going to discuss how to use jQuery to read a text file or text database. Sometimes we only need a text database when the data is not so sensitive or unimportant.  Let’s see how jQuery read text file.

jQuery.get() or $.get() Method

The jQuery.get() method simply send a request to the text file or other files and get the result back. In this case the text file must be stored in the same hosting account and the URL must be specified. Let’s take an example to see how to use jQuery.get() to read a text file.

<head>
<title>jQuery Read Text File</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $("document").ready(function() {

         $.get('http://localhost/xampp/jphp/jquery/urlData.txt', function(data) {

                    // Check if the result (data) can be passed back
                    alert(data);    
            });

    });
</script>

</head>
<body>

<h2>jQuery Read text File</h2>

</body>
</html>

In the above example:

  • the text file is a small database containing the urls of some websites.
  • the text file is stored in the same hosting of the web page.
  • the url of the text file must be specified.

The result will be as shown below:

Look! The contents of the text file has been passed back successfully.

jQuery example File:

Click here to download or view the file.

We got the contents of the text database. It now depends on how we like to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>