1、網絡地址URL的知識簡介:
URL : 統一資源定位符 (Uniform Resource Locator, URL)
完整的URL由這幾個部分構成:
scheme://host:port/path?query#fragment
scheme = 通信協議 (常用的http,ftp,maito等)
host = 主機 (域名或IP)
port = 端口號
path = 路徑
query = 查詢。可選,用於給動態網頁(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技術製作的網頁)傳遞參數,可有多個參數,用”&”符號隔開,每個參數的名和值用”=”符號隔開。
fragment = 信息片斷。字符串,用於指定網絡資源中的片斷。例如一個網頁中有多個名詞解釋,可使用fragment直接定位到某一名詞解釋。(也稱爲錨點.)
例如:一個這樣一個URL:http://www.master8.net:80/seo/?ver=1.0&id=6#imhere
2、用javascript獲得其中的各個部分
1, window.location.href :
整個URl字符串(在瀏覽器中就是完整的地址欄)
2,window.location.protocol:
URL 的協議部分(本例返回值:http:)
3,window.location.host:
URL 的主機部分(本例返回值:www.master8.net)
4,window.location.port:
URL 的端口部分(如果採用默認的80端口(update:即使添加了:80),那麼返回值並不是默認的80而是空字符本例返回值:”")
5,window.location.pathname:
URL 的路徑部分(就是文件地址)(本例返回值:/seo/)
6,window.location.search:
查詢(參數)部分,除了給動態語言賦值以外,我們同樣可以給靜態頁面,並使用javascript來獲得相信應的參數值(本例返回值:?ver=1.0&id=6)
7,window.location.hash: 錨點(本例返回值:#imhere).
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function gets(){
var name=Array();
var values=Array();
var star=window.location.search;
get = star.substr(1);
getarr=get.split("&");
for(i=0;i<getarr.length;i++)
{
slit = getarr[i].split("=");
name[i] = slit[0];
values[i] = slit[1];
}
for(i=0;i<name.length;i++)
{
if(name[i]=="name")
document.getElementById("sss").value=values[i];
}
}
//-->
</SCRIPT>
</HEAD>
<BODY οnlοad="gets()">
<INPUT TYPE="text" NAME="sss" id="sss" value="">
</BODY>
</HTML>