open-window.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * 打开窗口
  3. * @param {Sting} url
  4. * @param {Sting} title
  5. * @param {Number} w
  6. * @param {Number} h
  7. */
  8. export default function openWindow(url, title, w, h) {
  9. // Fixes dual-screen position Most browsers Firefox
  10. const dualScreenLeft =
  11. window.screenLeft !== undefined ? window.screenLeft : screen.left
  12. const dualScreenTop =
  13. window.screenTop !== undefined ? window.screenTop : screen.top
  14. const width = window.innerWidth
  15. ? window.innerWidth
  16. : document.documentElement.clientWidth
  17. ? document.documentElement.clientWidth
  18. : screen.width
  19. const height = window.innerHeight
  20. ? window.innerHeight
  21. : document.documentElement.clientHeight
  22. ? document.documentElement.clientHeight
  23. : screen.height
  24. const left = width / 2 - w / 2 + dualScreenLeft
  25. const top = height / 2 - h / 2 + dualScreenTop
  26. const newWindow = window.open(
  27. url,
  28. title,
  29. 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' +
  30. w +
  31. ', height=' +
  32. h +
  33. ', top=' +
  34. top +
  35. ', left=' +
  36. left
  37. )
  38. // Puts focus on the newWindow
  39. if (window.focus) {
  40. newWindow.focus()
  41. }
  42. }