skip to Main Content
<script>
  function sync_data() {
localStorage.setItem("syncDataClicked", true);
location.reload();
}

window.onload = function () {
    if (localStorage.getItem("syncDataClicked") === "true") {
        localStorage.removeItem("syncDataClicked");
        sync_data('show');
    }
};
</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
<input type="submit" name="submit3" onclick="sync_data()" value="Sync data">
</body>
</html>
Html: 
<input type="submit" name="submit3" onclick="sync_data()" value="Sync data">

JS:
function sync_data() {
    window.onload = function () {
    setTimeout(function () {
        sync_data('show');
    }, 5000);
    }
    }

This js is not working. After onclick and page reload I need to add the pop by function sync_data(). Is there any solution. I have updated with local storage but its showing the popup message

2

Answers


  1. Your window.onload function is already inside the sync_data function. So window.onload will not be triggered because the page is already loaded when the button is clicked. So your window.onload function should excute before excute the sync_data function. So inside the sync_data function you should include flag. According to that flag window.onload function will triggered. you can use localStorage to store a flag. This is my suggest code. I am not sure is it work or not. I was add custom pop up alert to code.

       <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Sync Data</title>
        <script>
            function sync_data() {           
                localStorage.setItem("syncDataClicked", true);           
                location.reload();
            }
    
            window.onload = function () {            
                if (localStorage.getItem("syncDataClicked") === "true") {               
                    localStorage.removeItem("syncDataClicked");                
                    showPopup();
                }
            };
    
            function showPopup() {           
                alert('Data synced!');
            }
        </script>
    </head>
    <body>
        <input type="submit" name="submit3" onclick="sync_data()" value="Sync data">
    </body>
    </html>
    
    Login or Signup to reply.
  2. You may use the following code:

    function sync_data(string) {
      localStorage.setItem("syncDataClicked", string);
      location.reload();
    }
    try{
    if (localStorage.getItem("syncDataClicked")){
      let popup = new Blob([localStorage.getItem("syncDataClicked")],{type: "text/html"});
      let link = document.createElement("a");
      let url = window.URL.createObjectURL(popup);
      link.href = url;
      link.target= "_blank";
      link.click();
      localStorage.removeItem("syncDataClicked");
    }
    }
    catch (e) {console.log("It's the first time"); }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Hello</title>
    </head>
    <body>
    <input type="submit" name="submit3" onclick="sync_data('show')" value="Sync data">
    </body>
    </html>

    Hope you find this helpful!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search