0

I have created a simple web component using vanilla js just onclick to open URL on a new window unfortunately I get the following error

my-component.js:24 Uncaught TypeError: window.open is not a function

Here is my web component:

export class MyComponent extends HTMLElement {
  constructor() {
    super();
  }

  connectedCallback() {
    this._render();
    this._attachEventHandlers();
  }

  _render() {
    const container = document.createElement("div");
    container.innerHTML = `
            <div class='app'>
                <button id="open-me">Open</button>
          </div>`;

    const shadowRoot = this.attachShadow({ mode: "open" });
    shadowRoot.appendChild(container);
    }

    _attachEventHandlers() {
        this.shadowRoot.getElementById('open-me').addEventListener('click', () => {
            window.open('www.google.com', "_blank");
        })
    }
}

window.customElements.define("my-component", MyComponent);

usage of this component, just import in your HTML and add it like this

 <my-component></my-component>

What is wrong here?

The Dead Man
  • 6,258
  • 28
  • 111
  • 193
  • Could this be because your browser is blocking pop-ups? No repro otherwise. – Sebastian Simon Sep 30 '21 at 23:35
  • @SebastianSimon u can copy the code and run it in your browser – The Dead Man Sep 30 '21 at 23:47
  • That’s what I did before commenting “No repro otherwise”. – Sebastian Simon Sep 30 '21 at 23:50
  • @SebastianSimon so ur saying its working on your side? and what do u mean no repro otherwise? – The Dead Man Sep 30 '21 at 23:52
  • “No [repro](//en.wiktionary.org/wiki/repro#Verb)” means “Not reproducible”. Yes, I’m saying that it’s working on my side. Could you please confirm if it works when you disable any pop-up blockers in your browser, if you have any? If `window.open` isn’t a function, what is it instead? – Sebastian Simon Sep 30 '21 at 23:58
  • @SebastianSimon still the same its weirdo though, check, https://i.stack.imgur.com/9I79k.png which browser ur using? – The Dead Man Oct 01 '21 at 00:01
  • Firefox Nightly 94.0a1, but it shouldn’t depend on the browser. Just check what `window.open` is. Almost certainly, `open` has been redefined, either by a variable, or by some element with `id="open"`. See [TypeError: window.open is not a function](/q/15019992/4642212) and [Do DOM tree elements with ids become global variables?](/q/3434278/4642212). – Sebastian Simon Oct 01 '21 at 00:08

1 Answers1

0

I would shorten it to:

customElements.define("my-component", class extends HTMLElement {
  constructor() {
    let button = document.createElement("button");
    button.innerText = "open";
    button.onclick = (evt) => {
      alert("You clicked! " + evt.target.nodeName);
      open('www.google.com', "_blank");
    };
    super().attachShadow({mode:"open"})
           .append(button);
  }
});
<my-component></my-component>
  • No need for a Named class definition, when you use it only once
  • no need for export, the Web Component is created in the Global registry
  • No need for the div soup, unless they actually have a purpose.
  • addEventListener only required when you want muliple events on the same element
  • window. for customElements, alert and open not required;
    we're in the Browser, the default is window
  • super sets AND returns the 'this' scope; so can be chained
  • attachShadow sets AND returns this.shadowRoot; so can be chained
  • Learn the power of append over appendChild
    https://developer.mozilla.org/en-US/docs/Web/API/Element/append

Your open issue can not be related to Web Components; check that statement raw, in the console.

Here in StackOverflow it says:

Postscript per comments

OP was responsible for overriding window.open himself.

Danny '365CSI' Engelman
  • 16,526
  • 2
  • 32
  • 49
  • thanks for an explanation but still this does not open URL in a new window and I have this error : `my-component.js:37 Uncaught TypeError: open is not a function` – The Dead Man Oct 01 '21 at 09:30
  • Like everyone in the comments says.. it is an issue on **your** environment; and most likely ``open`` was _overloaded/redefined_ (in your environment). That means only you can debug it; we do not have a magic glass sphere to peek in and see your solution. Use ``console.log( typeof window.open)`` to see what open now is (on your environment) Run ``open`` in the console, try it in another Tab, try it in other browsers... be a programmer. – Danny '365CSI' Engelman Oct 01 '21 at 09:39
  • problem is I was ovveriding open in my html when I was using the component – The Dead Man Oct 01 '21 at 10:06
  • YEAH! You solved it!! **programmer error** – Danny '365CSI' Engelman Oct 01 '21 at 10:26