简体   繁体   中英

Refresh page after clicking button (ok / cancel)

 function clicked() { if (confirm('You just clicked the button, click ok or cancel to refresh.')) { yourformelement.submit(); location.reload(); } else { return false; } }
 <button class="center" type="submit" style="border: 0; background: transparent"> <img src="Playbutton.png" width="800" height="400" alt="submit" onclick="clicked();" /> </button>

I want the page to refresh when someone clicks 'ok' or 'cancel'

2 things, first place the onclick handler on the button itself instead of on the image, second you didnt have a reload call in your else statement.

 const yourformelement = {}; yourformelement.submit = () => {}; function clicked() { if (confirm('You just clicked the button, click ok or cancel to refresh.')) { yourformelement.submit(); location.reload(); } else { location.reload(); return false; } }
 <button class="center" type="submit" style="border: 0; background: transparent" onclick="clicked()"> <img src="https://kriya-materials.com/wp-content/uploads/2018/01/Play-Button-Transparent-PNG.png" width="400" height="400" alt="submit"/> </button>

I think you don't need to use "if" statement

function clicked() {
    confirm('You just clicked the button, click ok or cancel to refresh.');
    yourformelement.submit();
    location.reload();
}

I recommend using an alert() for only displaying a message.

Change

location.reload();

to

window.location.reload();

and see if that accomplishes what you need.

  • your function works good but you just need to store your confirm() in variable then check when the user click ok it's return true and when click cancel it's return false so if you wants your confirm working in cancel and ok you need to check the returned value like the following
function clicked() {
    var conf = confirm('You just clicked the button, click ok or cancel to refresh.')
    console.log(conf);
     if (conf === true || conf === false) {
           yourformelement.submit();
           location.reload();
       } else {
           return false;
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM