我想用javascript在某些选项卡中打开新页面。这是我的HTML:
< head> < script src =// ajax.googleapis/ajax/libs/jquery/1.11.1/jquery.min.js>< / script> < script src =functions.js>< / script> < meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1/> < title>无标题文档< / title> < link href =css.css =stylesheettype =text / css/> < / head> < body> ... < label> < input type =imagename =imageFieldid =loginClientonclick =testUrl()src =images / index_33.gif/> < / label> ... < / body> < / html>这是我的functions.js:
function testUrl() { window.location =content.html; $ b函数loginClient() ... //更多函数我也尝试了其他方式:
window.location.replace(content。 html) top.location =content.html window.location.assign(content.html) 窗口。 open(content.html,_self) window.location.href =content.html window.event.returnValue = false; window.location =content.html; setTimeout(function(){window.location.assign(content.html); return false;},500)没有什么作用,我在Opera,Firefox(全部在Windows)和Chrome(Debian)上试用过。 但它工作如果我设置: $ p $ window.open(content.html)
但是在新标签中,并不是解决方案,我需要它在同一个标签中。
但是如果我试图用Firebug进行调试,并且总是点击进入而不是所有的解决方案。如果我在重定向之前设置了alert(),它也可以工作。
控制台显示没有错误。 我不知道,帮助。
已解决:感谢@lonesomeday!
解决方案:
$(document).ready(function(){ $(#loginClient)。 click(function(event){ event.preventDefault(); window.location =content.html; }); });解决方案
在< input type =image> 元素上的onclick 属性。图像输入实际上是 submit 按钮。当你点击它时,它被激活,并且表单被提交。当提交表单时,它将取消任何正在进行的HTTP请求,例如 window.location 调用。
你需要阻止表单提交。
快速和肮脏的方法是返回 false 从你的事件处理程序。就像这样:
onclick =return testUrl() $对于JS: 函数testUrl()$ b preb { window.location =content.html; 返回false; }
更漂亮的方法是使用Javascript绑定事件处理程序,使用 event.preventDefault 。
There are many same questions here. I tried all solutions - nothing helps.
I want to open new page in some tab with javascript. Here is my HTML:
<head> <script src="//ajax.googleapis/ajax/libs/jquery/1.11.1/jquery.min.js" ></script> <script src="functions.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <link href="css.css" rel="stylesheet" type="text/css" /> </head> <body> ... <label> <input type="image" name="imageField" id="loginClient" onclick="testUrl()" src="images/index_33.gif" /> </label> ... </body> </html>Here is my functions.js:
function testUrl() { window.location = "content.html"; } function loginClient() ...//more functionsI tried other ways too:
window.location.replace("content.html") top.location="content.html" window.location.assign("content.html") window.open("content.html", "_self") window.location.href="content.html" window.event.returnValue = false; window.location = "content.html"; setTimeout(function(){window.location.assign("content.html");return false;},500)nothing works, I tried it in Opera, Firefox (all in Windows) and Chrome (Debian). BUT it works if I set:
window.open("content.html")but in new tab, isn't solution, I need it in same tab.
BUT if I try to debug it with Firebug and click always "Step into" than all solution work. And if I set alert() before redirecting, it works too.
Console shows no errors. I have no idea, help.
RESOLVED: Thanks to @lonesomeday!
this is the solution:
$(document).ready(function(){ $("#loginClient").click(function(event){ event.preventDefault(); window.location = "content.html"; }); });解决方案
You've got this onclick attribute on an <input type="image"> element. Image inputs are effectively submit buttons. When you click it, it is activated, and the form is submitted. When a form is submitted, it cancels any ongoing HTTP requests, such as your window.location call.
So you need to prevent the form submission from going ahead.
The quick and dirty way to do this is to return false from your event handler. Something like this:
onclick="return testUrl()"With the JS:
function testUrl() { window.location = "content.html"; return false; }The prettier way to do this would be to bind your event handlers with Javascript and use event.preventDefault.
更多推荐
无论如何,JavaScript重定向无法正常工作
发布评论