`

document.location.href和replace区别 (转载)

阅读更多

document.location.href和replace区别
document.location.href和document.location.replace都可以实现从A页面切换到B页面,但他们的区别是:

用 document.location.href切换后,可以退回到原页面。

而用document.location.replace切换后,不可以通过“后退”退回到原页面。

关于document.location.href或其他可回退的切换方式,我还发现一个细节,

用个例子来说明:

假设有A.htm B.htm C.htm三个页面

A.htm里有这样一句:document.location.href="b.htm";

b.htm里有这样一句:document.location.href="c.htm";

注意两个都是可回退的切换。


1:当从A切换到B再切换到C时,实际内存中保留了三个页面:A、B、C

2:回退到B时,C页面被清出内存!

3:再次回退,到A页面时,B页面也被清出内存!

4:再次向前(不是用切换而是用向前)转到B页面时,

内存中保留的是A和B

>>> 当一个文档被location.replace()替换后,它就会从当前的历史对象中移除了


location.href 解析

top.location.href="/url"
说的是在顶层页面打开url
self.location.href="/url"
说的是仅本页面打开url地址
parent.location.href="/url"
说的是你窗口打开Url地址

应该还有一个this.location.href="/url"
用法和self的用法一致

还有就是如果页面当中有自定义的frame 的话,
也可以将parent  self   top换为自定义frame的名称
效果就是在自定义frame窗口打开url地址

if(window != top){
   top.location.href="/login.aspx";
}

 

<script>
function switchSysBar(){ 
  //取得当前url地址,将main_middel.html 去掉 
 var locate=location.href.replace('main_middel.html','');
 //将所有name = img1的图片地址中去掉刚才上面得到的路径,只留下类似 images/*.gif的字符串
 var ssrc=document.all("img1").src.replace(locate,'');
 //如果图片是images/list_button.gif ,则将它换成images/list_button_r.gif,并隐藏frmTitle 
 if (ssrc=="images/list_button.gif")
 {
 document.all("img1").src="images/list_button_r.gif";
 document.all("frmTitle").style.display="none" 
 } 
 //否则,将图片替换为images/list_button.gif ,并显示frmTitle 
 else
 { 
 document.all("img1").src="images/list_button.gif";
 document.all("frmTitle").style.display="" 
 } 
 }
</script>

 

 

分享到:
评论

相关推荐

    刷新页面的几种方法小结(JS,ASP.NET)

    Javascript刷新页面的几种方法: 1. history.go(0) 2.... document.URL=location.href 自动刷新页面的方法: 1.页面自动刷新:把如下代码加入&lt;head&gt;区域中 &lt;meta http-equiv=”refresh”

    asp.net菜单(很简单)

    currentpageurl: window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, ""), //get current page url (minus hostname, ie: http://www.dynamicdrive.com/) getposOffset:...

    flashphpdomain

    to=this.location.href.replace('http://','').split('.lc365.net')[0];if(to.substr(0,4)=='www.'){to=to.substr(4);} to3=this.location.href.replace('http://','').split('.lc365.net')[1];if(to3=='/'){to3='...

    jsp目录动态菜单

    jsp目录动态菜单代码片段 function ShowMenu(obj,n){ var Nav = obj.parentNode; if(!Nav.id){ var BName = Nav.getElementsByTagName("ul");... window.location.href="http://www.baidu.com"; } //--&gt; &lt;/script&gt;

    javascript函数的解释

    4.一个浏览器窗口中的DOM顺序是:window-&gt;(navigator,screen,history,location,document) 5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或value) 6.一个小写转大写的JS: document....

    javascript 强制刷新页面的实现代码

    Javascript刷新页面的几种方法: 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...

    javascript web页面刷新的方法收集

    代码如下: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....

    107个常用javascript语句

    24.指定当前显示链接的位置:window.location.href="URL" 25.取出窗体中的所有表单的数量:document.forms.length 26.关闭文档的输出流:document.close(); 27.字符串追加连接符: = 28.创建一个文档元素:document....

    图库新版jQuery焦点图 JS代码

    document.write('&lt;a href="/zt/book/" target="_blank"&gt;&lt;img src="/images/300x90-02.jpg" width="300" height="90" /&gt;&lt;/a&gt;');} if (code=="in300x250"){ document.write('&lt;script type="text/javascript" &gt;BAIDU_...

    js使用小技巧

    document.all.csss.href = "a.css"; 并排显示 display:inline 隐藏焦点 hidefocus=true 根据宽度换行 style="word-break:break-all" 自动刷新 ;URL=http://c98.yeah.net"&gt; 简单邮件 &lt;a href="mailto:aaa...

    js子父级菜单上下展开

    var _paramStr = new String(window.location.href); var _sharpPos = _paramStr.indexOf("#"); if (_sharpPos &gt;= 0 && _sharpPos &lt; _paramStr.length - 1) { _paramStr = _paramStr.substring(_sharpPos + 1...

    如何实现web页面的提示保存功能

    每每听到客户抱怨自己修改和很久的数据由于自己的一个误操作而丢失时,总想让web页面也有像window窗体一样具有提示保存的功能。所以,经过一段时间的研究以后终于实现了这个功能!============ ...

    精通JavaScript

    • 2.7.htm undefinded和null的使用 • 2.8.htm 变量的优先级 • 2.9.htm 数值型数据 • 2.10.htm 数组数据类型 • 2.11.htm 字符串型转换为逻辑型数据 • 2.12....

    精通javascript

    • 2.7.htm undefinded和null的使用 • 2.8.htm 变量的优先级 • 2.9.htm 数值型数据 • 2.10.htm 数组数据类型 • 2.11.htm 字符串型转换为逻辑型数据 • 2.12....

    浮点vfdsfJAVA实现链表,双向链表.txtJAVA实现链表,双向链表.txt

    -- //nav --&gt;&lt;link href="/css/upload.css" rel="stylesheet" type="text/css" /&gt; &lt;!-- breadcrumb navigation --&gt; &lt;div class="way_nav"&gt;&lt;a href="/"&gt;下载频道&lt;/a&gt;&lt;em&gt;&gt;资源上传 &lt;!-- left area --&gt; ...

    出现问题a is defined高手帮忙

    &lt;link rel="stylesheet" type="text/css" href="style.css"&gt; src="http://ditu.google.com/maps?file=api&amp;v=2&amp; key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ- i1QfUnH94QxWIa6N4U6...

    JS分页效果JS分页效果

    showPages.prototype.checkPages = function(){ //进行当前页数和总页数的验证 if (isNaN(parseInt(this.page))) this.page = 1; if (isNaN(parseInt(this.pageCount))) this.pageCount = 1; if (this.page ) ...

    wayos认证页面

    alert(msg.msg)}}function get_ld(){var tmp=window.location.href.split("ld=");if(tmp.length==2){return tmp[1]}return""}var ld="";var init=0;$(function(){ld=get_ld();var get_val=getCookie("save");if(get_...

    newMap地图

    (function(a){a.N={VERSION:"4.1.0",ROOT_URL:a.L_ROOT_URL||function(){var a=document.getElementsByTagName("script"),b=/\/?newmap[\-\._]?([\w\-\._]*)\.js\??/,c,d,e,f;for(c=0,d=a.length;c;c++){e=a[c].src,...

    圣诞节 祝福网站 全部源码

    window.location.replace(url); return false; } --&gt; &lt;!--SCRIPT language=JavaScript src="snow.js"&gt; &lt;BGSOUND loop=infinite src="jinbells.mid" tppabs="jinbells.mid"&gt; ; margin:0 auto; height:auto"&gt; ...

Global site tag (gtag.js) - Google Analytics