Javascript window.close() function

98 views Asked by At

function validateLoginForm(){
  var str = '';
  if(trim(document.getElementById('userName').value)=='')
   str = 'UserName is Required\r\n';
  if(trim(document.getElementById('password').value)=='')
   str = str+'Password is Required';
  if(str=='')
   return true;
  else{
   alert(str);
   return false; 
  }
   }
    
 function myOpenWindow() {
   var rand_no = Math.round(1000*Math.random());
  
   if(window.name == "IMSWindowName"){
      windowName = "IMSWindowNameFirst"+rand_no;
       windowName = "_self"+rand_no;
   }else{
      windowName = "IMSWindowName"+rand_no;
       windowName = "_self"+rand_no;
   }
   heightVal=screen.height-150;
   widthVal=screen.width-150;
  
   newwin2 = window.open('',windowName,'toolbar=no,status=0,resizable=1,scrollbars=1,top=0,left=0,height='+heightVal+',width='+widthVal+'');
   document.forms[0].target=windowName;
   document.forms[0].action='LogIn';
   document.forms[0].method='post';
   newwin2.focus();
   window.opener='X'; 
   window.open('','_parent',''); 
   window.close();
   }
<input type=submit class="myButton" value="Sign In" title="Ok" onClick="if(validateLoginForm()){myOpenWindow();quitBox('quit');}else{return false;}"/></td><td><input type=button class="myButton" value="Clear" title="Reset" onClick="document.forms[0].reset();"/>

I tried this code to automatically close the login window when it is logged in. This code works perfectly with IE browsers but failed to work in other browsers like chrome and firefox?

1

There are 1 answers

0
Madhavan.V On

You can use this syntax,

   if(document.getElementById('userName').value.trim()=='')

Check this link for more on String.prototype.trim()