HTML編程之js跨域抓取問題

對於網站建設來說,通常會需要遇到js跨域抓取的問題,那麼對於初學者來說,都不知道該怎麼來解決,對此,鄭州網站建設公司燚軒科技就來跟大家好好的分析解答一下吧。

源代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
var url;
 
url = window.location.href; /* 獲取完整URL */
alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */
 
url = window.location.pathname; /* 獲取文件路徑(文件地址) */
alert(url); /* /Test/index.html */
 
url = window.location.protocol; /* 獲取協議 */
alert(url); /* http */
 
url = window.location.host; /* 獲取主機地址和端口號 */
alert(url); /* http://127.0.0.1:8020/ */
 
url = window.location.hostname; /* 獲取主機地址 */
alert(url); /* http://127.0.0.1/ */
 
url = window.location.port; /* 獲取端口號 */
alert(url); /* 8020 */
 
url = window.location.hash; /* 獲取錨點(“#”後面的分段) */
alert(url); /* #test?name=test */
 
url = window.location.search; /* 獲取屬性(“?”後面的分段) */
alert(url);
 
/* 如果需要URL中的某一部分,可以自己進行處理 */
url = window.location.pathname;
url = url.substring(url.lastIndexOf('/') + 1, url.length);
alert(url); /* /index.html */
 
/* 
* 如果頁面使用了框架(frameset)
* 要獲取到指定頁面的URL
* 只要把window換成指定的頁面即可
*/
/* 'frame'爲指定頁面的class名 */
var url = window.parent.frames['frame'].location.href;
/* 獲取當前地址欄中顯示的URL */
var url = window.parent.location.href;
/* window parent 可互換 */
var url = parent.window.location.href;
</script>
</head>
<body>
</body>
</html>

現在,大家都知道如何有效的解決js跨域抓取的問題了吧,如果還是存在不理解的地方,可以留言諮詢鄭州網站建設公司燚軒科技,我們可以共同學習進步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章