简体   繁体   English

使用Javascript在弹出窗口中打开链接但弹出窗口应该在新窗口中

[英]Open link in Popup Window with Javascript but pop up should be in new window

I'm trying to send a popup window of specific dimensions which also centers in the screen. 我正在尝试发送一个特定尺寸的弹出窗口,该窗口也位于屏幕中心。

Here is my source: 这是我的来源:

 <li>
   <a class="google" href="http://www.google.com/" target="_blank" onclick="return windowpop(this.href, 545, 433)">Test</a>
 </li>

And JAVASCRIPT IS: 和JAVASCRIPT是:

function windowpop(url, width, height) {

    alert('hello');

    var leftPosition, topPosition;

    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);

    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);

    //Open the window.
    window.open(url, "Window2", "status=no,height=" + height + ",width=" + width + 
    ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + 
    leftPosition + ",screenY=" + topPosition + 
    ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
}

In this the pop up is generated. 在此,生成弹出窗口。 But I need that alert message in new window. 但是我需要在新窗口中显示警报消息。

But I need that alert message in new window. 但是我需要在新窗口中显示警报消息。

Since you are opening the same page ( this.href ), before calling the alert function, check if the current window has a parent (ie if it is a pop-up window): 由于您打开同一页面( this.href ),在调用 alert函数之前,检查当前窗口是否有父窗口(即它是否是弹出窗口):

    if (window.opener) {
    {
      alert('hello');
    }

UPDATE: 更新:

You should extract out the alert('hello'); 你应该提取alert('hello'); code from the windowpop function and put it directly in the <script> section so that the opened page will load it. 来自windowpop函数的代码并将其直接放在<script>部分中,以便打开的页面将加载它。

Like that: 像那样:

    <script>
    if (window.opener) {
    {
      alert('hello');
      ...
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM