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