獲取網頁URL地址及參數等的兩種方法(js和C#)


 轉:獲取網頁URL地址及參數等的兩種方法(js和C#)

一 js

先看一個示例

用javascript獲取url網址信息

<script type="text/javascript"> 
document.write("location.host="+location.host+"<br>"); 
document.write("location.hostname="+location.hostname+"<br>"); 
document.write("location.href="+location.href+"<br>"); 
document.write("location.pathname="+location.pathname+"<br>"); 
document.write("location.protocol="+location.protocol+"<br>"); 
</script>

執行 用javascript獲取url網址信息 代碼效果如下

location.host=www.vnde.cn 
location.hostname=www.vnde.cn 
location.href=http://www.vnde.cn/bc/2008/0306/article_1860.html 
location.pathname=/bc/2008/0306/article_1860.html 
location.protocol=http:

詳細介紹 window.location方法獲取URL

統一資源定位符 (Uniform Resource Locator, URL) 完整的URL由這幾個部分構成:

scheme://host:port/path?query#fragment

scheme:通信協議

常用的http,ftp,maito等

host:主機

服務器(計算機)域名系統 (DNS) 主機名或 IP 地址。

port:端口號

整數,可選,省略時使用方案的默認端口,如http的默認端口為80。

path:路徑

由零或多個”/”符號隔開的字符串,一般用來表示主機上的一個目錄或文件地址。

query:查詢

可選,用於給動態網頁(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技術制作的網頁)傳遞參數,可有多個參數,用”&”符號隔開,每個參數的名和值用”=”符號隔開。

fragment:信息片斷

字符串,用於指定網絡資源中的片斷。例如一個網頁中有多個名詞解釋,可使用fragment直接定位到某一名詞解釋。(也稱為錨點.)

示例:

1, window.location.href

整個URl字符串(在瀏覽器中就是完整的地址欄)

返回值:http://www.2astudio.com:80/view.asp?id=209#cmt1323

2,window.location.protocol

URL 的協議部分

返回值:http:

3,window.location.host

URL 的主機部分,

返回值:www.2astudio.com

4,window.location.port

URL 的端口部分。如果采用默認的80端口(update:即使添加了:80),那么返回值並不是默認的80而是空字符。

本例返回值:空

5,window.location.pathname

URL 的路徑部分(就是文件地址)

返回值:/view.asp

6,window.location.search

查詢(參數)部分。除了給動態語言賦值以外,我們同樣可以給靜態頁面,並使用javascript來獲得相信應的參數值

返回值:?id=209

7,window.location.hash

錨點

返回值:#cmt1323

二 C#

底下這張表就是各種跟 Browser Request 的網址相關的屬性與用法:

 

網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath /
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx
Request.Path /News/Press/Content.aspx/123
Request.RawUrl /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath /News/Press/Content.aspx/123
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1897
Request.Url.Authority localhost:1897
Request.Url.LocalPath /News/Press/Content.aspx/123
Request.PathInfo /123
Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1
Request.Url.Query ?id=1
Request.Url.Fragment  
Request.Url.Segments
News/ 
Press/ 
Content.aspx/ 
123


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM