IE8下,js調用另一頁面js方法,不能alert問題

src.html頁面的代碼:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>alert 在ie8與ie6下不同</title>
</head>
<body>
	<input type='button' id='openPage' value='openPage'>
	<script type='text/javascript'>
		var fun = function(){
			open("other.html");
		}
		var obj = document.getElementById("openPage");
		obj.onclick = fun;
		function showAlert(){
			window.alert("src.html");
			//window.confirm("你是否確定增加?");			
		}
	</script>
</body>
</html>

other.html頁面的代碼

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>alert 在ie8與ie6下不同</title>
</head>
<body>
	<input type='button' id='runOther' value='runOtherPageMethod'>
	<script type='text/javascript'>
		var fun = function(){
			window.opener.showAlert();
		}
		var obj = document.getElementById("runOther");
		obj.onclick = fun;
	</script>
</body>
</html>
用ie8打開src.html界面,點擊openPage按鈕,彈出other.html頁面,再點擊runOtherPageMethod按鈕,發現並沒有出現alert;

在ie6,chrome等其它瀏覽器下都會出現alert。


本人的解決方法:

重新寫方法替換alert與confirm,用模態窗口實現阻塞。

/**
*@param {String} msg  提示的信息
*/
function dhcsys_alert(msg){
	var userAgent = window.navigator.userAgent;
	if (userAgent.indexOf("MSIE 7.0")>0 || userAgent.indexOf("MSIE 8.0")>0) {
			window.showModalDialog("alert.html", 
				{msg:msg}, 
				"dialogWidth:300px;dialogHeight:120px;center:yes;toolbar=no;menubar:no;scrollbars:no;resizable:no;location:no;status:no;help:no;");	
	}else{
		alert(msg);
	}       
}
/**
*@param {String} msg  提示的信息
*@param {String} YesOrNo 默認焦點在Yes上還是No上 可選值爲YES或NO.默認爲Yes
*/
function dhcsys_confirm(msg,YesOrNo){
	var userAgent = window.navigator.userAgent;
	if(userAgent.indexOf("MSIE 7.0")>0 || userAgent.indexOf("MSIE 8.0")>0) {
		if(arguments.length==1){
			YesOrNo = "YES";
		}
		return window.showModalDialog("confirm.html", 
					{msg:msg,YesOrNo:YesOrNo}, 
					"dialogWidth:300px;dialogHeight:120px;center:yes;toolbar=no;menubar:no;resizable:no;location:no;status:no;help:no;");	
	}else{
		return window.confirm(msg);
	}
}

下面是alert.html

<html>
<head>
<title>提示信息</title>
</head>
<body style="overflow-y:hidden;">
	<table style="width:100%;height:100%;background-color=#c7dffc;">
		<tr>
			<td>
			<table  align="center" style="width:250px;height:50px;background='images/logon_bg.jpg'" border=0 cellSpacing=0 cellPadding=0>
				<tr>
					<td><img src="../skin/default/images/icon-warning.gif"/></td>
					<td>
						<div id="DivMsg"></div>
					</td>
				</tr>
			</table>
			</td>
		</tr>		
		<tr>
			<td align="center"><input type="button" id="BtnSure" value="確認" style="width:50px;height:22px;"></td>
		</tr>
	</table>
	
	<script type="text/javascript">
		window.onload = function(){
			var divObj = document.getElementById("DivMsg");
			var btnObj = document.getElementById("BtnSure");
			if (typeof dialogArguments!="undefined" && dialogArguments) {
				divObj.innerHTML = dialogArguments.msg;
			}else{
				divObj.innerHTML = "請爲dhcsys_alert方法傳入提示信息";
			}
			btnObj.onclick = function(){
				window.close();	
			};
			if (btnObj) {
				try {
					btnObj.focus();
					//btnObj.select();
				} catch(e) {}
			}
		}
	</script>
</body>
</html>

confirm.html

<html>
<head>
<title>確認信息</title>
</head>
<body style="overflow-y:hidden;" >
	<table style="width:100%;height:100%;background-color:#c7dffc;">
		<tr>
			<td>
			<table  align="center" style="width:250px;height:20px;" border=0 cellSpacing=0 cellPadding=0>
				<tr>
					<td><img src="../skin/default/images/icon-question.gif"/></td>
					<td>
						<div id="DivMsg"></div>
					</td>
				</tr>
			</table>
			</td>
		</tr>		
		<tr>
			<td align="center"><input type="button" id="BtnYES" value="是" style="width:50px;height:22px;"> <input type="button" id="BtnNO" value="否" style="width:50px;height:22px;"></td>			
		</tr>
	</table>
	
	<script type="text/javascript">
		window.onload = function(){
			var divObj = document.getElementById("DivMsg");
			var btnYESObj = document.getElementById("BtnYES");
			var btnNOObj = document.getElementById("BtnNO");
			var defaultFocus = "YES";
			if (typeof dialogArguments!="undefined" && dialogArguments && dialogArguments.msg) {
				divObj.innerText = dialogArguments.msg;
				defaultFocus = dialogArguments.YesOrNo;
			}else{
				divObj.innerHTML = "請爲dhcsys_confirm方法傳入提示信息";
			}
			btnYESObj.onclick = function(){
				returnValue = true;
				window.close();	
			};		
			btnNOObj.onclick = function(){
				returnValue = false;
				window.close();	
			};
			if(defaultFocus=="NO"){
				btnNOObj.focus();
			}else{
				btnYESObj.focus();
			}			
		}
		window.onunload = function(){
			if(!returnValue) returnValue = false;
		}
	</script>
</body>
</html>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章