window.showModalDialog部分浏览器不兼容,尝试使用window.open()封装代替,需要打开子窗口并向父窗口传递数据。
测试对话框子窗口返回数据:window.returnvaluewindow.open替换showModalDialog页面A.htm,使用window.open弹出页面B.htm,在页面B.htm上选择一个值,返回关闭窗口.htm后将选定的值传给父窗口A。A.htm得到返回值后,给这个页面的文本框赋值。A.htmltitle>测试对话框
B另一种封装方法modal.jsvarhas_showModalDialog=!!window.showModalDialog;//定义一个全局变量判断是否有原生的showModalDialog方法if(!has_showModalDialog&&!!(window.opener)){window.onbeforeunload=function(){window.opener.hasOpenWindow=false;//当弹窗关闭时,告诉opener:它的子窗口已经关闭}}//如果window.showModalDialog不存在就定义if(window.showModalDialog==undefined){window.showModalDialog=function(url,mixedVar,features){if(window.hasOpenWindow){警报("Youhaveopenedawindow!Pleasefirstprocessit");//避免多次点击弹出多个窗口window.myNewWindow.focus();}window.hasOpenWindow=true;if(mixedVar)varmixedVar=mixedVar;//因为window.showmodaldialog和window.open的参数不同,封装时使用正则表达式格式化参数if(features)varfeatures=features.replace(/(dialog)|(px)/ig,"").replace(/;/g,',').replace(/\:/g,"=");//window.open("Sample.htm",null,"height=200,width=400,status=yes,toolbar=no,menubar=no");//window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px");varleft=(window.screen.width-parseInt(features.match(/width[\s]*=[\s]*([\d]+)/i)[1]))/2;vartop=(window.screen.height-parseInt(features.match(/height[\s]*=[\s]*([\d]+)/i)[1]))/2;window.myNewWindow=window.open(url,"_blank",features);}}A.html父页面测试对话框内容哈哈哈