-3
我想讓JavaScript讀取包含指向網站的鏈接的txt文件,並且可以按下按鈕以發送到隨機網站...但我的代碼不會工作中,我已經嘗試了很多東西......我的代碼不會工作
繼承人我的代碼
<script>
var sites = [];
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function() {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
var allText = rawFile.responseText;
alert(allText);
callback(allText);
}
}
}
rawFile.send(null);
}
readTextFile("file:../Nicklas Behrend/Desktop/links.txt", filesText =>{
sites = filesText.split('\n');
});
function randomSite() {
var i = parseInt(Math.random() * sites.length);
location.href = sites[i];
}
</script>
在這個部分它說:「表達式語句不轉讓或叫」
readTextFile("file:../Nicklas Behrend/Desktop/links.txt", filesText =>{
sites = filesText.split('\n');
});
當我按下按鈕,我做了,我到一個網站,說「 404找不到「
不能使ajax調用文件協議....需要成爲一個網絡路徑 – charlietfl
好吧,對不起,但我很新這個......我該怎麼做? – NicklasBehrend
如果文件與頁面位於同一目錄中...只是使用文件名 – charlietfl