【问题标题】:What is the Javascript code to open this link in a New window?在新窗口中打开此链接的 Javascript 代码是什么?
【发布时间】:2012-03-22 21:16:55
【问题描述】:

我喜欢这个按钮以在新窗口中打开网站。现在它在同一页面中打开。 下面的代码是我的 html 中的代码。 onclick 事件会调用 setMyAdd 函数。

<input width="167" type="image" height="45" value="Click me" src="/portals/0/schedappnt.png" id="btn3" onclick="setMyAdd(); return false;" />

这就是我的 javascript 中的内容:

<script language="javascript">
var myLink = "";
function hideMe() {
    document.getElementById('btn3').style.visibility='hidden';
}
function setMyAdd() {
    location.href=myLink;
}
function checkForChange() {
    // var buttonSelected=selList.value;
    var buttonSelected = document.getElementById('selList').value; 

    // alert("Option Selected is : " + buttonSelected );
    if (buttonSelected=="optx") {
    myLink = "/HOME/ScheduleanAppointment/tabid/83/Default.aspx";
    document.getElementById('btn3').style.visibility='visible';
    } else {
    myLink = "http://offsiteschedule.zocdoc.com/remote/schedulePopup.aspx?professionalId=0&amp;providerId=10754&amp;locationId=20113&amp;repeatPatient=True";
    document.getElementById('btn3').style.visibility='visible';
    }
}
</script>

<script> 

我需要修改什么才能在新窗口中打开?

【问题讨论】:

标签: javascript function href window.open


【解决方案1】:

将location.href = myLink更改为window.open(myLink);

【讨论】:

    【解决方案2】:
    function setMyAdd() {
        window.open(myLink,'Advertisement');
    }
    

    第二个参数是要打开的窗口的名称。如果您希望广告每次都在同一个外部窗口中打开,而不是产生无穷无尽的新窗口,请指定窗口名称。否则亚伦的答案是正确的。

    【讨论】:

      【解决方案3】:

      您可以通过位置获取有关当前 url 的信息。因此,使用您的代码更改当前窗口的 url,网站通常会在同一个窗口中打开。

      你需要使用 window 对象的 open() 方法告诉浏览器用你想要的 url 打开一个新窗口。这意味着

      函数 setMyAdd() { window.open(myLink); }

      您还有其他机会使用 open() 方法,例如指定新窗口的大小。您可以找到信息here。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多