user23266137
user23266137

Reputation: 11

window.alert works after clearing cache in safari

I'm building an extension for safari.

I have window.alert in content.js. It works most of the time, but sometimes it doesn't. When it doesn't work, I clear cache of safari and it starts working again.

I want to make it work all the time without clearing cache.

How can I achieve it?

content.js:

window.addEventListener("load", async (event) => {
  console.log("loading");
  try {
     window.alert("alert");
 }
}
)

I googled but I couldn't find any related questions about this problem.

Upvotes: 0

Views: 45

Answers (1)

user23266137
user23266137

Reputation: 11

I could solve this error. The reason why alert was not displayed is because "load" is not applicable when its from cache. See this.

So I changed from "load" to "pageshow". My whole code is as below.

window.addEventListener("pageshow", async (event) => {
  console.log("loading");

  if (event.persisted) {
    console.log("displayed from cache");
    window.location.reload();
  } 
})

Then it worked!

Upvotes: 1

Related Questions