1 function downloadfile(url) { 2 var xmlHttp = null; 3 if (window.ActiveXObject) { 4 // IE6, IE5 瀏覽器執行代碼 5 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 6 } else if (window.XMLHttpRequest) { 7 // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執行代碼 8 xmlHttp = new XMLHttpRequest(); 9 } 10 //2.如果實例化成功,就調用open()方法: 11 if (xmlHttp != null) { 12 xmlHttp.open("get", url, true); 13 xmlHttp.send(); 14 xmlHttp.onreadystatechange = doResult; //設置回調函數 15 } 16 function doResult() { 17 if (xmlHttp.readyState == 4) { //4表示執行完成 18 if (xmlHttp.status == 200) { //200表示執行成功 19 //引用js庫:http://danml.com/js/download2.js 20 download(xmlHttp.responseText, "error.txt", "text/plain"); 21 } 22 } 23 } 24 }