最近在使用 window的location时碰到一个无法跳转页面的问题,

后来在location语句后加了一条这样的语句:window.event.returnValue = false;
然后竟然可以正常跳转了,具体原因还在学习中,稍后更新。

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="get">
<label for="inp" id="label">
tel:<input id="inp" type="text" placeholder="enter your tel">
</label><br/>
<button id="btn1" onclick="jData()">submit</button>
</form> <script type="text/javascript">
var input=document.getElementById("inp");
function jData(){
var pattr=new RegExp(/\d{11}/);
var res=pattr.test(input.value);
if(res){
window.location.assign("http://www.baidu.com");
window.event.returnValue = false;
}
}
</script>
</body>
</html>

后来发现,这个onlick是在form表单里。莫非这里页面跳转与form的action冲突?

后来试了去掉form标签,不用添加window.event.returnValue = false也可以正常跳转。

至于为什么form表单影响到了location,以后学习了再分析。

以下是没有使用form的code,chrome/firfox测试了没问题

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<label for="inp" id="label">
tel:<input id="inp" type="text" placeholder="enter your tel">
</label>
<span id="span"></span>
<br/>
<button id="btn1" onclick="jData()">submit</button> <script type="text/javascript">
var input=document.getElementById("inp");
var label=document.getElementById("label");
var span=document.getElementById("span");
function jData(){
var pattr=new RegExp(/\d{11}/);
var res=pattr.test(input.value);
if(res){
// window.location.href="http://www.baidu.com";
window.location.assign("http://www.baidu.com");
// window.event.returnValue = false;
}else{
span.innerHTML="the tel num wrong,pls enter again";
span.style.color="red";
}
}
</script>
</body>
</html>

window.location无法跳转页面的问题的更多相关文章

  1. 遇到问题-----JS中设置window.location.href跳转无效(在a标签里或这form表单里)

    问题情况 JS中设置window.location.href跳转无效 代码如下: ? 1 2 3 4 5 6 7 8 <script type="text/javascript&quo ...

  2. javascript 中设置window.location.href跳转无效问题解决办法

    javascript 中设置window.location.href跳转无效问题解决办法 问题情况 JS中设置window.location.href跳转无效 原因是 a标签的href跳转会执行在wi ...

  3. window.location.href跳转无效

    window.location.href跳转无效     问题情况 JS中设置window.location.href跳转无效   原因是 a标签的href跳转会执行在window.location. ...

  4. 关于window.location不跳转的问题

    今天在码代码的时候遇到个问题:html里采用onclick事件来实现window.location = url的跳转,在内嵌元素上又加上了href="javascrit:;"的属性 ...

  5. 使用location.href跳转页面在火狐浏览器中报错404

    HTML文件中引入外部js文件,在该js文件里用window.location.href跳转相对路径下的html地址,火狐浏览器会报错404,而谷歌浏览器却显示正常·,分析了一下原因:在识别相对路径时 ...

  6. window.location.href跳转问题

    任务中遇到这样一个问题,用window.location.href跳转一到个网址,但是每次都出错,显示网址前面加上了文件所在文件夹的路径 示例如下: window.location.href=&quo ...

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

    一.最外层top跳转页面,适合用于iframe框架集 top.window.location.href("${pageContext.request.contextPath}/Login_g ...

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

    1.window.location=url; window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. 一.最外层top跳转页面,适合用于iframe框架集 ...

  9. window.location.href跳转问题2

    "window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一 ...

随机推荐

  1. swift菜鸟入门视频教程-12-21讲

    前段时间录制的swift教程.一直懒就没有发出来,有些教程是在xcode beta2的时候录制的,所以可能如今不太适合了,可是还是会有点帮助,懒了不想写太多的东西了,基本上就是依照官方文档上面的章节做 ...

  2. 70.nodejs操作mongodb

    转自:https://www.cnblogs.com/whoamme/p/3467374.html 首先安装nodejs mongodb npm install mongodb var mongodb ...

  3. POJ 2138 最长路

    思路: 如果j能由i得到 则i向j连一条边 答案就是最长路的末节点所代表的string //By SiriusRen #include <cstdio> #include <cstr ...

  4. python3 requests 模块 json参数和data参数区别

    json 表示使用application/json方式提交请求 data 使用application/form-urlencode方式提交请求

  5. Python爬虫之『urlopen』

    本文以爬取百度首页为示例来学习,python版本为python3.6.7,完整代码会在文章末附上 本次学习所用到的python框架:urllib.request 本次学习所用到的函数: urllib. ...

  6. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析

    AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...

  7. Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer

    A computer system is described having one or more host processors, a host chipset and an input/outpu ...

  8. POJ——T 2796 Feel Good

    http://poj.org/problem?id=2796 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15375   ...

  9. GCC中-fpic解惑(转载)

    参考: 1.<3.18 Options for Code Generation Conventions>2.<Options for Linking>3.<GCC -fP ...

  10. hdoj-1870-愚人节的礼物(栈)

    愚人节的礼物 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...