后台调用前台的javascript函数不成功
这个市关闭页面的函数
function Close()
{
var ua=navigator.userAgent
var ie=navigator.appName== "Microsoft Internet Explorer "?true:false
if(ie)
{
var IEversion=parseFloat(ua.substring(ua.indexOf( "MSIE ")+5,ua.indexOf( "; ",ua.indexOf( "MSIE "))))
if(IEversion < 5.5)
{
var str = ' <object id=noTipClose classid= "clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 "> '
str += ' <param name= "Command " value= "Close "> </object> ';
document.body.insertAdjacentHTML( "beforeEnd ", str);
document.all.noTipClose.Click();
}
else
{
window.opener =null;
window.close();
}
}
else
{
window.close()
}
}
因为不能在后台调用这个函数,于是我把他放在后台中,但是不能成功,这是为什么啊?下面是改了的后台代码:
string js=@ " <script language= 'javascript '> ";
js+= "var ua=navigator.userAgent; ";
js+= "var ie=navigator.appName== 'Microsoft Internet Explorer '?true:false; ";
js+= "if(ie) ";
js+= "{ ";
js+= "var IEversion=parseFloat(ua.substring(ua.indexOf( 'MSIE ')+5,ua.indexOf( '; ',ua.indexOf( 'MSIE ')))); ";
js+= "if(IEversion < 5.5) ";
js+= "{ ";
js+= "var str= ' <object id=noTipClose classid= 'clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 '> '; ";
js+= "str+= ' <param name= 'Command ' value= 'Close '> </object> '; ";
js+= "document.body.insertAdjacentHTML( 'beforeEnd ',str); ";
js+= "document.all.noTipClose.Click(); ";
js+= "} ";
js+= "else ";
js+= "{ ";
js+= "window.opener =null; ";
js+= "window.close(); ";
js+= "} ";
js+= "} ";
js+= "else ";
js+= "{ ";
js+= "window.close(); ";
js+= "} ";
js+= " </script> ";
Response.Write(js);
[解决办法]
那是因为 先执行后台代码才执行 页面代码的
[解决办法]
Page.RegisterStartupScript( "onLoad ", " <script> Close(); </script> ");//在后台调用前台的javascript函数
[解决办法]
因为涉及到页面之间传值,我用下面的代码可以关闭页面(不过有的时候会提示“是否要关闭”),并向我的父窗口传值。
string js=@ " <script> ";
js+= "window.opener.GetFavs( ' "+favs+ " '); ";
js+= "window.close(); ";
js+= " </script> ";
Response.Write(js);
这段window.close()前面加上这句就可以了
window.opener =null;
[解决办法]
RegisterStartupScript是服务器端调用客户端的脚本的时候来调用用的,