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,')

新しいオープニングウィンドウを特定のサイズで開きたい。高さと幅を指定するにはどうすればよいですか?

回答:


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 = 400pxではなくwidth = 400)。

ほとんどのブラウザでは、改行なしで記述されていないと機能しません。変数が設定されると、すべてが1行になります。

<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
また、最後の「)」文字を「); returnfalse;」に交換することもできます。ポップアップに加えて元のリンクが開かれないようにするため。
アンドリュー

2
古いものですが、検索でこれを見つけたので、@ AndrewSpearからの返信に従って回答を修正しました
neil

1
@Larry Hipp画面のサイズに合わせて変更するにはどうすればよいですか?
Idham Choudry 2016年

@IdhamChoudry width / heightプロパティを削除するだけで、使用可能なすべてのスペースが自動的に使用されます。設定width=100vw, height=100vhもうまくいくと思います。
vadorequest 2017年

1
私にとっては機能しますが、重要なことの1つです。上記の例のように、関数の本体で改行を使用しないでください。改行を削除しましたが、うまくいきました。
ユージーン


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 Developer Networkの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>
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.