zoukankan      html  css  js  c++  java
  • 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直接定位到某一名词解释。(也称为锚点.)

    对于这样一个URL

    http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere


    我们可以用javascript获得其中的各个部分
    1, window.location.href
          整个URl字符串(在浏览器中就是完整的地址栏)
          本例返回值: http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

    2,window.location.protocol
          URL 的协议部分
          本例返回值:http:

    3,window.location.host
          URL 的主机部分
          本例返回值:www.x2y2.com

    4,window.location.port
          URL 的端口部分
          如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
          本例返回值:""

    5,window.location.pathname
          URL 的路径部分(就是文件地址)
          本例返回值:/fisker/post/0703/window.location.html

    6,window.location.search
          查询(参数)部分
          除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
          本例返回值:?ver=1.0&id=6

    7,window.location.hash
          锚点
          本例返回值:#imhere <src="http://feeds.feedburner.com/~s/fisker?i=http://www.x2y2.com/fisker/post/0703/window.location.html" type="text/javascript" charset="utf-8">

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------

    window.location.href/replace/reload()--页面跳转+替换+刷新

    一、最外层top跳转页面,适合用于iframe框架集

    top.window.location.href("${pageContext.request.contextPath}/Login_goBack");

    ============================================================================================

    二、window.location.href和window.location.replace的区别 

    1.window.location.href=“url”:改变url地址; 

    2.window.location.replace(“url”):将地址替换成新url,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,
    因此当使用replace方法之后,你不能通过“前进”和“后 退”来访问已经被替换的URL,这个特点对于做一些过渡页面非常有用!

    三、强制页面刷新 

    1.window.location.reload():强制刷新页面,从服务器重新请求! 

    reload() 方法用于重新加载当前文档。
    如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已改变。如果文档已改变,reload() 会再次下载该文档。如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。

    我们都知道客户端浏览器是有缓存的,里面存放之前访问过的一些网页文件。
    其实缓存里存储的不只是网页文件,还有服务器发过来的该文件的最后服务器修改时间。
    If-Modified-Since是标准的HTTP请求头标签,在发送HTTP请求时,把浏览器端缓存页面的最后修改时 间一起发到服务器去,服务器会把这个时间与服务器上实际文件的最后修改时间进行比较。
    如果时间一致,那么返回HTTP状态码304(不返回文件内容),客户端接到之后,就直接把本地缓存文 件显示到浏览器中。
    如果时间不一致,就返回HTTP状态码200和新的文件内容,客户端接到之后,会丢弃旧文件,把新文件 缓存起来,并显示到浏览器中。


    ============================================================================================

    四、window.location.reload();页面实现跳转和刷新 

    1 history.go(0)
    2 location.reload()
    3 location=location
    4 location.assign(location)
    5 document.execCommand('Refresh')
    6 window.navigate(location)
    7 location.replace(location)
    8 document.URL=location.href
    这几个都可以刷新
    window.location.reload();刷新
    window.location.href=window.location.href;刷新
    window.close();关闭窗口,不弹出系统提示,直接关闭 
    window.close()相当于self属性是当前窗口
    window.parent.close()是parent属性是当前窗口或框架的框架组
    页面实现跳转的九种方法实例:

     1 <html>
     2 <head>
     3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     4 <title>navigate</title>
     5 <script language="javascript">
     6     setTimeout('window.navigate("top.html");',2000);
     7     setTimeout('window.document.location.href="top.html";',2000);
     8     setTimeout('window.document.location="top.html";',2000);
     9     setTimeout('window.location.href="top.html";',2000);
    10     setTimeout('window.location="top.html";',2000);
    11     setTimeout('document.location.href="top.html";',2000);              
    12     setTimeout('document.location="top.html";',2000);
    13     setTimeout('location.href="top.html";',2000);
    14     setTimeout('location.replace("top.html")',2000);
    15     //window对象
    16         //document对象
    17             //location对象
    18                 //href属性
    19                 //1.window.document.location.href
    20                 //2.window.document.location
    21                 //3.window.location.href
    22                 //4.window.location
    23                
    24                 //5.document.location.href
    25                 //6.document.location
    26                 //7.location.href
    27                 //8.window.navigate
    28                 //9.location.replace
    29                 //只要使用location方法,和任意的window对象,location对象,href属性连用,都可以页面的跳转//// 
    30 </script>
    31 </head>
    32 
    33 <body>
    34 页面将在2秒后跳转
    35 </body>
    36 </html>



    解释:
    location是个对象,比如本页的document.location和window.location的属性有    
      location.hostname   =   community.csdn.net 
      location.href   =   http://community.csdn.net/Expert/topic/4033/4033372.xml?temp=2.695864E-02 
      location.host   =   community.csdn.net 
      location.hash   =   
      location.port   =   
      location.pathname   =   /Expert/topic/4033/4033372.xml 
      location.search   =   ?temp=2.695864E-02 
      location.protocol   =   http: 
      可见href是location的属性,类别是string。

  • 相关阅读:
    关于p标签
    用unescape反编码得出汉字
    一个未知高度垂直居中的简单方法
    发现个div float的小秘密
    w3cschool关于list-style-position时的另外发现
    oracle 11gR2默认密码修改
    程序员的十楼层。看看自己在第几层
    Steve Yegge:Google面试秘籍
    为学Linux,我看了这些书
    程序员的困境
  • 原文地址:https://www.cnblogs.com/gxp69/p/7135900.html
Copyright © 2011-2022 走看看