Open this window.open in a centralized window

1

I have the following script that takes all links from a page and opens in a window, How can I open the result in a centralized window?

javascript: var w = window.open('', '', 'height=800, width=600');var a = document.getElementsByTagName('a');var b = a.length; if(b != 0){ w.document.write('<h1> Lista de Links </h1> '); for (i = 0; i < b; i++){ w.document.write('<pre><a href=\'' + a[i] + '\'>' + a[i] + '</a>' + '</pre> ');}} else{ w.document.write('Nenhum link encontrado');}
    
asked by anonymous 11.04.2018 / 23:40

1 answer

2

Take the width of the window with window.innerWidth , subtract the width of the open window ( 600 ) and divide by 2.

The result of this puts you in the left property of the open window:

window.open('', '', 'height=800, width=600, left='+(window.innerWidth-600)/2);

If you want to center vertically too, it's the same thing, only using window.innerHeight with the top property:

window.open('', '', 'height=800, width=600, left='+(window.innerWidth-600)/2+', top='+(window.innerHeight-800)/2);
    
12.04.2018 / 00:01