windowOpen.ts 561 B

123456789101112131415161718192021222324
  1. export const windowOpen = (
  2. url: string,
  3. type?: "_blank" | "_parent" | "_self" | "_top"
  4. ) => {
  5. const a = document.createElement("a");
  6. a.setAttribute("href", url);
  7. a.setAttribute("target", type || "_blank");
  8. a.rel = "noreferrer";
  9. document.body.appendChild(a);
  10. if (a.click) {
  11. a?.click();
  12. } else {
  13. try {
  14. let evt = new Event("click", {
  15. bubbles: false,
  16. cancelable: true,
  17. });
  18. a.dispatchEvent(evt);
  19. } catch (error) {
  20. window.open(url, type || "_blank");
  21. }
  22. }
  23. document.body.removeChild(a);
  24. };