您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“JS獲取網(wǎng)頁源代碼”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
JS 獲取網(wǎng)頁源代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>遠(yuǎn)程網(wǎng)頁源代碼讀取</title> <style type="text/css"> /* 頁面字體樣式 */ body, td, input, textarea { font-family:Arial; font-size:12px; } </style> <script type="text/javascript"> //用于創(chuàng)建XMLHttpRequest對象 function createXmlHttp() { //根據(jù)window.XMLHttpRequest對象是否存在使用不同的創(chuàng)建方式 if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); //FireFox、Opera等瀏覽器支持的創(chuàng)建方式 } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器支持的創(chuàng)建方式 } } //直接通過XMLHttpRequest對象獲取遠(yuǎn)程網(wǎng)頁源代碼 function getSource() { var url = document.getElementById("url").value; //獲取目標(biāo)地址信息 //地址為空時提示用戶輸入 if (url == "") { alert("請輸入網(wǎng)頁地址。"); return; } document.getElementById("source").value = "正在加載……"; //提示正在加載 createXmlHttp(); //創(chuàng)建XMLHttpRequest對象 xmlHttp.onreadystatechange = writeSource; //設(shè)置回調(diào)函數(shù) xmlHttp.open("GET", url, true); xmlHttp.send(null); } //將遠(yuǎn)程網(wǎng)頁源代碼寫入頁面文字區(qū)域 function writeSource() { if (xmlHttp.readyState == 4) { document.getElementById("source").value = xmlHttp.responseText; } } </script> </head> <body> <h2>遠(yuǎn)程網(wǎng)頁源代碼讀取</h2> <div> 地址:<input type="text" id="url"> <input type="button" onclick="getSource()" value="獲取源碼"> </div> <textarea rows="10" cols="80" id="source"></textarea> </body> </html>
“JS獲取網(wǎng)頁源代碼”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。