JavaScript 页面跳转的几种方式

第一种:      
<script language="javascript" type="text/javascript">  
       window.location.href="login.jsp?backurl="+window.location.href;  
</script>  


第二种:      
<script language="javascript">  
       alert("返回");  
       window.history.back(-1);     
</script>  


第三种:     
<script language="javascript">  
       window.navigate("top.jsp");    
</script>  


第四种: 
    
<script language="JavaScript">            
       self.location=’top.htm’;     
</script>  


第五种:     
<script language="javascript">            
       alert("非法访问!");            
       top.location=’xx.jsp’;     
</script> 
 
=====javascript中弹出选择框跳转到其他页面=====  
<script language="javascript">  
<!--  
function logout()...{  
if (confirm("你确定要注销身份吗?是-选择确定,否-选择取消"))...{  
window.location.href="logout.asp?act=logout"  
}  
}  
-->  
</script> 
 
=====javascript中弹出提示框跳转到其他页面=====  
<script language="javascript">  
<!--  
function logout()...{  
alert("你确定要注销身份吗?");  
window.location.href="logout.asp?act=logout"  
}  
-->  
</script> 

window.location="";和 location.replace("");有什么区别?

这两个都能让网页导向令一个网址,那么有什么区别呢?比如能带参数,不能带参数之类的.

 Replace?还是Reload()?

好像没什么区别吧?没试过

  

replace(),reload()是重新加载本页,而replace()可以导向另外一个URL

 

给你举个例子:

我们现在有3个页面(a.html, b.html, c.html).

默认打开a.html页面,然后在a.html页面中通过一个链接转向a.html页面。

现在,我在b.html页面中用window.location.replace("c.html");与用window.location.href("c.html");分别进入c.html页面.

从用户界面来看是没有什么区别的,但是现在c.html页面有一个“返回”按钮,

用window.location.href("c.html");进入c.html页面时,

c.html页面中的调用window.history.go(-1);wondow.history.back();进入c.html页面时,一点这个"返回"按钮就要返回b.html页面的话,

而如果用window.location.replace("c.html");进入c.html页面的话,

c.html页面中的调用window.history.go(-1);wondow.history.back();方法是不好用的,会返回到a.html.

 

因为window.location.replace("c.html");是不会向服务器发送请求而进行跳转,而window.history.go(-1);wondow.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的,所以会跳到系统默认页面a.html 。

window.location.href("c.html");是向服务器发送请求的跳转,window.history.go(-1);wondow.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的,所以就可以返回到b.html。



参考推荐:

javascript跳转页面


在HTML页面中,阻止页面跳转通常是为了防止用户在进行某些操作(如提交表单)时页面刷新或跳转到其他页面,从而影响用户体验。以下是几种常见的阻止页面跳转方式: 1. 在HTML表单中使用JavaScript: 在表单提交时,可以通过JavaScript的`event.preventDefault()`方法阻止表单的默认提交行为。例如,在表单的提交事件处理器中调用此方法。 ```html <form onsubmit="event.preventDefault();"> <!-- 表单内容 --> </form> ``` 2. 在JavaScript事件处理器中使用return false: 当使用JavaScript为元素绑定了点击等事件处理器时,可以在处理器的函数中返回`false`来阻止默认行为。 ```html <a href="example.html" onclick="return false;">点击这里</a> ``` 3. 使用Ajax进行数据提交: 在不刷新页面的情况下,通过Ajax技术(如使用jQuery的`$.ajax`方法)提交数据到服务器,并处理响应,从而避免了页面跳转。 ```javascript $.ajax({ url: 'example.php', type: 'POST', data: { name: 'John', location: 'Boston' }, success: function(response) { // 处理服务器响应 } }); ``` 4. 在表单提交中使用AJAX: 可以在表单的`onsubmit`事件处理器中使用AJAX提交表单数据,然后阻止表单的默认提交行为。 ```html <form id="myForm" onsubmit="event.preventDefault(); submitForm(); return false;"> <!-- 表单内容 --> </form> <script> function submitForm() { // 使用AJAX提交表单数据 // ... } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值