![](/img/trans.png)
[英]Why can't separate the function for xmlHttp.onreadystatechange?
[英]why onreadystatechange function don't work in Windows
我當時正在研究ubuntu,腳本工作非常順利。 在兩個OS中,他都調用腳本,但在Windows中,他不參與onreadystatechange函數
這是我的腳本和HTML。
function readTextFile(file,str) { 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; if(allText.search(str) != -1){ alert("exist"); alert(location.pathname); }else{ alert("not exist"); } } } } rawFile.send(null); }
<html><head> <meta http-equiv="content-type" content="text/html; charset=windows-1252"> <title>Title</title> <script language="Javascript" src="C:/Users/to125286/Desktop/python_scraping/js/search.js"></script> </head> <body> <div> <form name="search" onsubmit="return readTextFile('file:///C:/Users/to125286/Desktop/python_scraping/web/All-html.txt',this.string.value);"> <input name="string" onchange="n = 0;"> <input type="submit" value="Go"> </form> </div> <table border="1" cellspacing="2" cellpadding="3"> <tbody> <tr> <th>Member Name</th> <th>Birth-Death</th> </tr> <tr> <td><a href="">ADAMS, George Madison</a></td> <td>1837-1920</td> </tr> <tr> <td><a href="">ALBERT, William Julian</a></td> <td>1816-1879</td> </tr> <tr> <td><a href="">ALBRIGHT, Charles</a></td> <td>1830-1880</td> </tr> </tbody> </table> <h1>Member</h1> </body>
我想知道與OS是否有關系,您為什么要這么做?
謝謝!
您正在嘗試使用XMLHttpRequest讀取磁盤上的文件( file:///
URL)。 許多網絡瀏覽器都可以防止這種情況,因為這可能會帶來安全風險。 Windows上使用的任何瀏覽器都可能阻止它。
如果您使用的是Chrome,則可以:
chrome.exe
實例正在運行,否則此技巧將不起作用。 --allow-file-access-from-files
命令行選項啟動chrome.exe
,以暫時禁用該限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.