获取网页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 |
获取网页URL地址及参数等的两种方法(js和C#)的更多相关文章
- 获取网页url中的参数
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript如何获取网页url中的参数
我们可以自定义一个公共函数来实现网页url中的参数获取,返回的是一个数组 GetUrlRequest: function () { var url = decodeURI(location.searc ...
- js获取当前url地址及参数
介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...
- JS获取当前完整的url地址以及参数的方法
javascript 获取当前 URL 参数的两种方法: //返回的是字符串形式的参数,例如:class_id=3&id=2& function getUrlArgStr(){ var ...
- 转:Web页面通过URL地址传递参数常见问题及检测方法
Web页面即我们在浏览器中所看到的网页,在Web应用程序中,其页面往往需要进行动态切换和数据交互,页面间的数据常规传递方法有多种,本文主要介绍Web页面处理程序中常见的URL地址参数传递方法,包括概述 ...
- php获取当前url地址的方法小结
js 获取: top.location.href //顶级窗口的地址 this.location.href //当前窗口的地址 php获取当前url地址: #测试网址: http://localhos ...
- PHP 获取完整URL地址
/** * 获取当前完整URL * @return string */ function get_url() { $sys_protocal = isset($_SERVER['SERVER_PORT ...
- 可操纵网页URL地址的js插件-url.js
url.js是一款能够很有用方便的操纵网页URL地址的js插件.通过url.js你能够设置和获取当前URL的參数,也能够对当前URL的參数进行更新.删除操作.还能够将当前URL的參数显示为json字符 ...
- 后台接收URL地址的参数
其实很简单,只是写一下加强记忆 后台接收URL传递过来的参数有两种方法: 第一种用request接收 HttpServletRequest request = ServletActionContext ...
随机推荐
- mysql样例数据库employees
Oracle和sqlserver都有基于员工信息的样例数据库,同样mysql上也是有的. 给出一个连接地址https://github.com/datacharmer/test_db. 下载后直接调用 ...
- UI学习笔记---第三天
视图控制器 功能:视图大小变换 \布局视图\响应事件\检测处理内存警告\检测以及处理屏幕旋转\检测视图切换 MVC (model View controller) UIViewController是M ...
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 软件工程课程作业(二)--四则运算2改进版(c++)
题目要求: 1.避免题目重复 2.可定制(数量/打印方式) 3.可以控制下列参数 (1)是否有乘除法 (2)是否有括号 (3)数值范围 (4)加减有无负数 (5)除法有无余数 关键设计思想: oper ...
- HDU-5781 ATM Mechine(概率DP)
题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...
- Java——网络编程
// TODO Auto-generated method stub //获取本地主机IP对象 InetAddress ip = InetAddress.getLocalHost(); Syst ...
- (转) 解决ssh的"Write failed: Broken pipe"问题
解决ssh的"Write failed: Broken pipe"问题 问题场景 服务器环境:阿里云 Linux CentOS 主机 客户端:Mac OSX Terminal ...
- nginx和apache配置目录浏览功能
今天工作需要,要给客户提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让客户看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件: 我们都知道在apache下可以配置 ...
- python tornado框架使用
处理方法 t_handler.py from tornado.web import RequestHandler class IndexHandler(RequestHandler): def get ...
- PHP使用mail()函数发送邮件流程以及注意事项
1. 配置 php.ini sendmail_path = /usr/sbin/sendmail -f yourname@sth.com -t -i 2. 使用函数 mail($to, $subjec ...