Prevent refreshing / reloading a page with JavaScript

前端 未结 2 719
无人共我
无人共我 2021-01-06 08:40

I know this is not recommended, but I need it because I have an iframe inside the page who has the actual content and I want that when the users hits refresh button, iframe

2条回答
  •  抹茶落季
    2021-01-06 09:28

    Returning false in an onsubmit event prevents the form from being submitted and stays in the same page without any user interaction..

    For example..

    when a form having input field is empty and submitted this javascript check for validation and returns accordingly.. If false is returned nothing is done(i.e. page is not refreshed or redirected)..

    If the validation is ok and if true is returned the form action is performed..

    function validateForm(Str) {
    mystring=document.getElementById('inputid').value; 
      if(!mystring.match(/\S/)){
        alert ('Please Enter a String.Field cannot be empty');
      return false;
      }else{
        return true;
      }
    }
    

提交回复
热议问题