javascript window.location 使用按钮 onclick 截断 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10199808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location truncating URL with button onclick
提问by William W
When setting window.location with a button onclick event, the result is being truncated after the second parameter name.
当使用按钮 onclick 事件设置 window.location 时,结果在第二个参数名称之后被截断。
This code:
这段代码:
<button onclick="window.location='index.php?p=reports_manage&id=new'">create new report - button</button>
Is sending the the user to this page:
正在将用户发送到此页面:
https://foo.com/index.php?p=reports_manage&id=
Even if I add extra parameters at the end it still gets cut off at the same place.
即使我在最后添加了额外的参数,它仍然会在同一个地方被切断。
All of these work fine however:
但是,所有这些都可以正常工作:
<a href="index.php?p=reports_manage&id=new">create new report - link</a>
<a href="javascript:window.location='index.php?p=reports_manage&id=new'">create new report - JS1</a>
<a href="javascript:void(0);" onclick="window.location='index.php?p=reports_manage&id=new'">create new report - JS2</a>
Any idea what could be causing this? As far as I can tell the button was working fine earlier and broke without any change to this code. What else could be affecting this? There are no javascript errors on the page and it happens regardless of what browser is used.
知道是什么原因造成的吗?据我所知,该按钮之前工作正常,并且在没有更改此代码的情况下就损坏了。还有什么会影响这个?页面上没有 javascript 错误,无论使用什么浏览器都会发生。
回答by trickyzter
A long shot, but maybe specifying the button type will help. In this case type="button".
一个长镜头,但也许指定按钮类型会有所帮助。在这种情况下,type="button"。
@William: Different browsers have different pre-requisites, however as a safeguard it's always good to be explicit. ;)
@William:不同的浏览器有不同的先决条件,但是作为保护措施,明确总是好的。;)
回答by GillesC
Use <input type="button" />
instead of <button></button>
as it has unexpected behaviour (and if use to submit a form it will even submit its content!)
使用<input type="button" />
而不是<button></button>
因为它有意外行为(如果用于提交表单,它甚至会提交其内容!)