Skip to content

Using window.onresize and document.querySelector is unsafe #3

@smalluban

Description

@smalluban

Question

It's not a bug, so I've chosen the "question" template. I looked at the source code, and I've found some worrying parts:

document.querySelector("instagram-widget").shadowRoot.querySelector(".instagram-widget-photos").innerHTML = html;

Your code won't work if there will be more elements on the same page. It will always update the first one. You can just reference your element by this - so instead querying document to access shadowRoot, you can use this.shadowRoot (after initializing it).

window.onresize = () => {

This is unsafe and can break other code. You assume that the user won't use window.onresize syntax, but he can, and then your callback is gone. You should use window.addEventListener().

if (self.options["force-square"] === "yes") {

Because of that, the onresize will only refer to the last instance of your component, so the above setting will apply from the last instance.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions