read txt file via client javascript

后端 未结 4 2123
小蘑菇
小蘑菇 2020-12-15 11:23

I\'m new to javascript and trying to open a txt file into var and then inject it to html div... I tried to use fopen but I didn\'t succeed.



        
4条回答
  •  春和景丽
    2020-12-15 12:07

    Although it says xml request this works perfectly fine for txt files too (server and client side).

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","YOUR_FILE.txt",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseText;
    

提交回复
热议问题