Open this window.open in a centralized window

Asked

Viewed 1,573 times

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');}

1 answer

2


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

The result of this you put on the property left 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 property top:

window.open('', '', 'height=800, width=600, left='+(window.innerWidth-600)/2+', top='+(window.innerHeight-800)/2);
  • It has to center Vertically too?

  • 1

    I added in the reply. :)

  • Show! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.