【问题标题】:open a new window inside the current window with javaScript [duplicate]使用javaScript在当前窗口内打开一个新窗口[重复]
【发布时间】:2014-08-01 09:08:56
【问题描述】:

我正在尝试做一个提示,它将引导我到另一个站点,但我希望另一个站点替换我所在的当前窗口。

代码:

<button id="butt">press</button>

<script>
document.getElementById('butt').onclick = function()
{
var age = parseInt(prompt("please enter your age", "18")); //parseInt turns the string to numbers
   if(age >= 18)
   {
       window.open('http://www.walla.co.il','walla');
   }
 }  

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您可以设置新的 location.href。

    window.location.href = 'http://www.walla.co.il'
    

    【讨论】:

      【解决方案2】:

      你可以使用替换方法:

      document.getElementById('butt').onclick = function()
      {
      var age = parseInt(prompt("please enter your age", "18")); //parseInt turns the string to numbers
         if(age >= 18)
         {
             window.location.replace('http://www.walla.co.il','walla');
         }
      }
      

      【讨论】:

        【解决方案3】:
        window.open("http://www.walla.co.il","_self");
        

        上面一行在同一页面中打开了 url。

        【讨论】:

        • 我试过了,它会在提示符中打开窗口。无论如何,人们已经告诉了我上面的答案,但是 ty
        猜你喜欢
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-21
        • 1970-01-01
        • 2017-07-13
        • 2014-08-29
        相关资源
        最近更新 更多