Home > Web Front-end > JS Tutorial > body text

Javascript combined with ajax to read the content of txt file_javascript skills

WBOY
Release: 2016-05-16 16:28:56
Original
1719 people have browsed it

The code is very concise, so there will be no more nonsense here, just go to the source code

html code

Copy code The code is as follows:




         


                                                                                                                                                                                                                       .                                      
                                                                                                    function show(){
Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},function(){alert('failed');})
            };
                                                                                         




javascript code

Copy code The code is as follows:

function Ajax(url,fnSucc,fnFaild)
                 {
//1. Create ajax object
If(window.XMLHttpRequest)
                                                                                                                                                                                                                                            {// code for IE7, Firefox, Chrome, Opera, Safari
                        var oAjax=new XMLHttpRequest();
                 }
Else                                                                                                                   {// code for IE6, IE5
                      var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
                 }
//2. Link to the server (open the connection to the server)
//open(method, file name, asynchronous transmission)
                   oAjax.open('GET',url,true);
//3.Send
                  oAjax.send();
//4.Receive return
oAjax.onreadystatechange=function()
                                                       {
If (oAjax.readyState==4)
                                                               {
If(oAjax.status==200)
                                                                                                                                                                                                                                                                                                             through fnSucc(oAjax.responseText);
                                                                                                       }                                                      else                                                                                                                                                                                                                                                                                                    through fnFaild(oAjax.status);
                                                                                                       }                        };
                  };
              }



The requested file is read.txt

Fill in the content as you like
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!