html-对于div内的js,window.location.href无效,而window.open有效

问题描述

对于div内的js,window.location.href无效,而window.open有效

为什么放入div中的submit按钮触发的onclick的js中window.location.href = "http://www.baidu.com";无效,而window.open("http://www.qq.com", "_blank");有效呢?我想网页重定向,应该怎么改呢?
以下是HTML代码:

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>跳转</title>
</head>

<script type="text/javascript">
    function jstiaozhuan() {
        window.location.href = "http://www.baidu.com";
        window.open("http://www.qq.com", "_blank");
    }
</script>

<body>
<div id="formContainer">
    <form id ="aa" method="ttt">
    <input type="submit" name="submit" value="跳转" onclick="jstiaozhuan()" value="sign">
    </form>
</div>
</body>

</html>

解决方案

window.location.href= "http://www.baidu.com" 无效是因为跨域的问题。所以只能用window.open或者用iframe

解决方案二:

要return false阻止表单的提交,要么type="submit" 改为type="button"

 <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>跳转</title>
</head>

<script type="text/javascript">
    function jstiaozhuan() {
        window.location.href = "http://www.baidu.com";
        //window.open("http://www.qq.com", "_blank");
        return false
    }
</script>

<body>
    <div id="formContainer">
        <form id="aa" method="ttt">
            <input type="submit" name="submit" value="跳转" onclick="return jstiaozhuan();" value="sign">
        </form>
    </div>
</body>

</html>

解决方案三:

JS window.open()

时间: 2024-08-22 14:59:47

html-对于div内的js,window.location.href无效,而window.open有效的相关文章

IE6浏览器中window.location.href无效的解决方法_javascript技巧

本文实例讲述了IE6浏览器中window.location.href无效的解决方法.分享给大家供大家参考.具体方法如下: window.location.href是js中跳转功能,很多人在ie6中都会发现window.location.href不能跳转了,下面我给大家来介绍一下其原因与解决方法. 问题代码如下: 复制代码 代码如下: <a href="javascript:void(0);" onclick="javascript:test();">点击

IE6浏览器中window.location.href无效解决办法

问题代码如下:  代码如下 复制代码 <a href="javascript:void(0);" onclick="javascript:test();">点击会跳转</a>  <script>  test = function(){    window.location.href = "http://www.111cn.net";  }  </script>   正确代码如下:  代码如下 复制代

[转载]window.location.href 失效的解决办法

原文地址:window.location.href 失效的解决办法作者:rapheal_Guo window.location.href 有时会失效..这又是万恶的IE的BUG..   微软上公布3个解决方案的. 原文地址:http://support.microsoft.com/kb/190244/en-us 第一种: 在window.location.href 后面加上 window.event.returnValue = false; 如: <a href="#" oncl

javascript-关于js,jquery问题,return false与window.location.href

问题描述 关于js,jquery问题,return false与window.location.href 我给每个 <a> 都加了个window.location.href,然后给一个a标签返回return false,但是不能取消 还是跳转了? $("#remove").click(function(){ var $tr = $(this).parent().parent(); var title = $.trim($tr.find("td:first"

关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别_javascript技巧

"window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一层页面跳转 "top.location.href"是最外层的页面跳转 举例说明: 如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写 "window.location.href"."locatio

快速解决js中window.location.href不工作的问题_javascript技巧

E6中在html中<a>标识中通过JS添加click事件调用一个JS函数,例如: < script type = "text/javascript" > function jump () { window . location . href = 'http://www.jb51.net' ; } function enjoy () { return false ; } < /script> html代码: <a href= "java

javascript-js window.location.href 跳转显示404错误,但是路径是对的

问题描述 js window.location.href 跳转显示404错误,但是路径是对的 地址是对的,但是服务器却显示404错误, 要手动刷新一下,页面才显示出来. 这是为什么啊 用C#后台写的:Response.Write("window.location.href='SHPage.html?sName=" + jumpStore + "'"); 别说什么地址错误,页面找不到. 如果是这样的话,网址不变,为什么网页重新刷新就正常显示了.这就不是地址错误了吧,地

url-关于window.location.href的URL访问的问题

问题描述 关于window.location.href的URL访问的问题 定义了一个变量,变量也拿到了,弹整个URL的时候也是对的,但是访问的时候地址栏中显示的却没了这个变量,求大神赐教啊!折磨了我整整一个晚上 解决方案 建议楼主看一下是不是js报错:EL表达是在js上的使用,我建议是这样的: var isBorrowIdNull = '${not empty borrowId}'; if('true' == isBorrowIdNull){ //执行borrowId不为空时的代码块... }

javascript window.location.href 跳转问题

问题描述 javascript window.location.href 跳转问题 请先看代码片段: if(1>0){ window.location.href="a.html"; } if(1<0){ window.location.href="b.html"; }else{ window.location.href="c.html"; } 为什么不跳转 a.html,反而跳转到了c.html 解决方案 http://www.jb5