fare clic su finestra aperta e dimensioni specifiche


87

Ho un collegamento come questo:

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

Voglio che la nuova finestra di apertura si apra in una dimensione specifica. Come si specificano l'altezza e la larghezza?

Risposte:


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>

Dove larghezza e altezza sono pixel senza unità (larghezza = 400 non larghezza = 400 px).

Nella maggior parte dei browser non funzionerà se non è scritto senza interruzioni di riga, una volta che le variabili sono state impostate hanno tutto in una riga:

<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
Dovrai anche scambiare l'ultimo carattere ")" con "); return false;" per evitare che il collegamento originale venga aperto oltre al popup.
Andrew

2
Uno vecchio ma l'ho trovato tramite la ricerca, quindi risposta corretta come da risposta di @AndrewSpear
neil

1
@ Larry Hipp come posso cambiarlo per adattarlo alle dimensioni dello schermo?
Idham Choudry

@IdhamChoudry Basta rimuovere le proprietà larghezza / altezza e prenderà automaticamente tutto lo spazio disponibile. Credo che anche l'impostazione width=100vw, height=100vhfunzionerebbe.
Vadorequest

1
Per me funziona, ma UNA COSA IMPORTANTE: non dovresti usare interruzioni di riga nel corpo della tua funzione, come nell'esempio sopra. Ho rimosso le interruzioni di riga e ha funzionato per me.
Eugene


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

12

Basta aggiungerli alla stringa del parametro.

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>

Anche se chiedo, cosa aggiunge questo a tutte le risposte già fornite?
EWit

3

Queste sono le migliori pratiche dalla pagina window.open di Mozilla Developer Network :

<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

Chiunque cerchi un componente file Vue veloce, ecco qui:

// 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>

Utilizzo:

<window-url url="/print/shipping" class="btn btn-primary" height="250" width="250">
    Print Shipping Label
</window-url>
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.