2013-04-24 14 views
11

Sto provando a caricare un href in una finestra popup di dimensioni specifiche che si concentra anche sullo schermo.Apri collegamento in finestra popup con Javascript

Qui è la mia fonte:

<li> 
    <a class="sprite_stumbleupon" href="http://www.stumbleupon.com/submit?url=http://www.your_web_page_url" target="_blank" onclick="return windowpop(545, 433)"></a> 
</li> 

Ed ecco il mio javascript:

function windowpop(url, width, height) { 
    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"); 
} 

Questo sembra restituire un errore 404 durante il test. Che cosa sto facendo di sbagliato?

Grazie mille.

+2

non è ancora avvenuto il parametro 'url' alla funzione' windowpop'. –

+0

'function windowpop (url, width, height)' La funzione si aspetta che tu restituisca un URL ma stai solo restituendo larghezza e altezza. – Mal

+0

Questo potrebbe essere chiuso per una buona ragione, ma l'ho trovato pazzo utile. Non conoscevo nessuno di quei parametri extra su window.open. – jstaab

risposta

12

function windowpop(url, width, height) La funzione richiede che venga restituito un URL.

onclick="return windowpop(545, 433)" Si sta solo restituendo il width e height.

Prova restituzione del URL utilizzando this.href:

onclick="return windowpop(this.href, 545, 433)" 

esempio:http://jsfiddle.net/zKKAM/1/

+2

Solo una nota, stavo usando questo metodo, e funziona bene se non permetti i popup sul tuo browser, tuttavia, se lo fai, aprirà il popup due volte. L'ho risolto aggiungendo event.preventDefault(); al clic del collegamento e rimuovi il ritorno dall'inizio di windowpop(). – madagalbiati

Problemi correlati