【问题标题】:javascript : window.location.href/window.open is not working on iframejavascript:window.location.href/window.open 在 iframe 上不起作用
【发布时间】:2016-11-07 18:03:44
【问题描述】:

更新代码

<div>
    <button type="button" class="submit btn btn-default" id="btnSubmit">Submit 
    </button>
    <button type="button">Cancel</button>
</div>




<script>
$("#btnSubmit").click(function(e)
 {
  e.preventDefault();

  //btn Click validation and code submission goes here..

   alert("Form has been successfully submitted");
   window.open("http://....../viewmyrequest.aspx","_self"); 

  }

</script>

window.location.href 仅在调试模式下工作,如果我关闭调试模式,则不会重定向到其他窗口。

我已经探索了许多具有相同主题的 StackOverflow 问题,但我没有得到任何可靠的答复。

window.location.href = "http://testwebsite.com/viewtickets.aspx"

我也尝试过其他选择

window.location = "http://testwebsite.com/viewtickets.aspx"
window.location.replace = "http://testwebsite.com/viewtickets.aspx"

【问题讨论】:

  • 什么不起作用?
  • window.location 重定向不起作用
  • 好的,那么是什么触发了代码运行?
  • 按钮点击 iframe。
  • 按钮 html 是什么样的?

标签: javascript jquery angularjs ajax html


【解决方案1】:

试试这个。在我所有的网站上都可以跨浏览器工作。

loc = "http://testwebsite.com/viewtickets.aspx";
    window.open(loc,'_self');

编辑: 这可能对你更有效..

<script>
function setURL(url){
    document.getElementById('iframe').src = url;
}
</script>
<iframe id="iframe" src="idreesinc.com/research.html" />
<input type="button" onclick="setURL('URLHere')" />

它将正确的 URL 插入 iframe。

【讨论】:

  • 我已经尝试了您的建议,但如果不启用调试模式仍然无法正常工作。
  • 我忘了告诉你我正在做这个按钮点击哪个触发器 window.open 在 iframe 上。
【解决方案2】:

我 99% 确定问题在于您使用的是提交按钮,并且提交的表单不允许您的 JavaScript 运行。因此,您需要将类型设置为按钮而不是提交按钮,或者您需要取消单击操作。

<button type="button" ... >

<button onclick="foo(); return false;" ... >

【讨论】:

  • 谢谢@epascarello。我已从“提交”更改为“按钮”。但是当调试模式关闭时它仍然不起作用。
  • 如果我第二次单击该按钮,它可以工作。不知道出了什么问题
  • 我已经用代码更新了我的问题。提前感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 2012-08-02
相关资源
最近更新 更多