onclick打开窗口和特定大小


87

我有这样的链接:

<a href="/index2.php?option=com_jumi&amp;fileid=3&amp;Itemid=11" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,')

我希望新的打开窗口以特定大小打开。如何指定高度和宽度?

Answers:


177
<a href="/index2.php?option=com_jumi&amp;fileid=3&amp;Itemid=11"
   onclick="window.open(this.href,'targetWindow',
                                   `toolbar=no,
                                    location=no,
                                    status=no,
                                    menubar=no,
                                    scrollbars=yes,
                                    resizable=yes,
                                    width=SomeSize,
                                    height=SomeSize`);
 return false;">Popup link</a>

其中width和height是没有单位的像素(width = 400而不是width = 400px)。

在大多数浏览器中,如果没有没有换行符就将其编写,那么一旦设置了变量,所有内容都将在一行中,它将无法正常工作:

<a href="/index2.php?option=com_jumi&amp;fileid=3&amp;Itemid=11" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=SomeSize,height=SomeSize'); return false;">Popup link</a> 

14
您还希望将最后一个“)”字符替换为“);返回false;”。以防止在弹出窗口之外打开原始链接。
安德鲁

2
通过搜索,以便更正答案旧的,但我发现这是每回复来自@AndrewSpear
尼尔

1
@Larry Hipp我如何更改它以适合屏幕尺寸?
艾德姆(Idham Choudry),2016年

@IdhamChoudry只需删除width / height属性,它将自动占用所有可用空间。我相信设置width=100vw, height=100vh也会起作用。
Vadorequest '17

1
对我来说,它有效,但是一件重要的事情-您不应该在函数主体中使用换行符,如上例所示。我删除了换行符,它对我有用。
尤金(Eugene)


20
window.open('http://somelocation.com','mywin','width=500,height=500');

12

只需将它们添加到参数字符串中即可。

window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=250')

11
<a style="cursor:pointer"
  onclick=" window.open('http://YOUR.URL.TARGET','',' scrollbars=yes,menubar=no,width=500, resizable=yes,toolbar=no,location=no,status=no')">Your text</a>

尽管我确实要问,这对所有已经给出的答案有什么补充?
EWit 2014年

3

这些是Mozilla开发人员网络window.open页面中的最佳做法:

<script type="text/javascript">
var windowObjectReference = null; // global variable

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("http://www.spreadfirefox.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>

<p><a
 href="http://www.spreadfirefox.com/"
 target="PromoteFirefoxWindowName"
 onclick="openFFPromotionPopup(); return false;" 
 title="This link will create a new window or will re-use an already opened one"
>Promote Firefox adoption</a></p>

0

任何需要快速Vue文件组件的人,这里您都可以:

// WindowUrl.vue

<template>
    <a :href="url" :class="classes" @click="open">
        <slot></slot>
    </a>
</template>

<script>
    export default {
        props: {
            url: String,
            width: String,
            height: String,
            classes: String,
        },
        methods: {
            open(e) {
                // Prevent the link from opening on the parent page.
                e.preventDefault();

                window.open(
                    this.url,
                    'targetWindow',
                    `toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=${this.width},height=${this.height}`
                );
            }
        }
    }
</script>

用法:

<window-url url="/print/shipping" class="btn btn-primary" height="250" width="250">
    Print Shipping Label
</window-url>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.