Javascript window.close()函数

时间:2017-11-20 11:57:42

标签: javascript jsp



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();"/>
&#13;
&#13;
&#13;

我尝试使用此代码在登录时自动关闭登录窗口。此代码与IE浏览器完美配合但无法在Chrome和Firefox等其他浏览器中运行?

1 个答案:

答案 0 :(得分:0)

您可以使用此语法

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

点击此链接了解有关String.prototype.trim()

的更多信息