// JScript 文件
function sAlert(txt,obj){
//var eSrc=(document.all)?window.event.srcElement:arguments[1];
var eSrc=obj;
var shield = document.createElement("DIV");
shield.id = "shield";
shield.style.position = "absolute";
shield.style.left = "0px";
shield.style.top = "0px";
shield.style.width = "100%";
shield.style.height = document.body.scrollHeight+"px";
shield.style.background = "#333";
shield.style.textAlign = "center";
shield.style.zIndex = "10000";
shield.style.filter = "alpha(opacity=0)";
shield.style.opacity = 0;
var alertFram = document.createElement("DIV");
alertFram.id="alertFram";
alertFram.style.position = "absolute";
alertFram.style.left = "50%";
alertFram.style.top = "50%";
alertFram.style.marginLeft = "-225px" ;
alertFram.style.marginTop = -75+document.documentElement.scrollTop+"px";
alertFram.style.width = "450px";
alertFram.style.height = "150px";
alertFram.style.background = "#ccc";
alertFram.style.textAlign = "center";
alertFram.style.lineHeight = "150px";
alertFram.style.zIndex = "10001";
strHtml = " <ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
strHtml += " <li style=\"background:#DD828D;text-align:left;padding-left:20px;font-size:14px;font-weight:bold;height:25px;line-height:25px;border:1px solid #F9CADE;\">[系统提示] </li>\n";
strHtml += " <li style=\"background:#fff;text-align:center;font-size:12px;height:120px;line-height:120px;border-left:1px solid #F9CADE;border-right:1px solid #F9CADE;\">"+txt+" </li>\n";
strHtml += " <li style=\"background:#FDEEF4;text-align:center;font-weight:bold;height:25px;line-height:25px; border:1px solid #F9CADE;\"> <input type=\"button\" value=\"确 定\" id=\"do_OK\" onclick=\"doOk()\" /> </li>\n";
strHtml += " </ul>\n";
alertFram.innerHTML = strHtml;
document.body.appendChild(alertFram);
document.body.appendChild(shield);
this.setOpacity = function(obj,opacity){
if(opacity>=1)opacity=opacity/100;
try{ obj.style.opacity=opacity; }catch(e){}
try{
if(obj.filters.length>0&&obj.filters("alpha")){
obj.filters("alpha").opacity=opacity*100;
}else{
obj.style.filter="alpha(opacity=\""+(opacity*100)+"\")";
}
}catch(e){}
}
var c = 0;
this.doAlpha = function(){
if (++c > 20){clearInterval(ad);return 0;}
setOpacity(shield,c);
}
var ad = setInterval("doAlpha()",1);
this.doOk = function(){
alertFram.style.display = "none";
shield.style.display = "none";
document.body.removeChild(alertFram);
document.body.removeChild(shield);
eSrc.focus();
document.body.onselectstart = function(){return true;}
document.body.oncontextmenu = function(){return true;}
}
document.getElementById("do_OK").focus();
eSrc.blur();
document.body.onselectstart = function(){return false;}
document.body.oncontextmenu = function(){return false;}
}
apsx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="JavaScript" src="divalert.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="点击这里" onclick="sAlert('渐变的',this);" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Literal ID="Literal1" runat="server"></asp:Literal></div>
</form>
</body>
</html>
cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string js = @"var xxx = function (){
sAlert('渐变的',document.getElementById('" + Button1.ClientID + @"'))
}
if(document.all)
{
window.attachEvent('onload', xxx);
}
else
{
window.addEventListener('load', xxx , false);
}";
ClientScript.RegisterStartupScript(GetType(), "js", js, true);
}
}
解释
ClientScript.RegisterStartupScript
这个方法 是向前台输出一段代码,不是一定是js脚本的,只是现在都习惯用它输出js脚本
当指定它的第4个参数addScriptTags 为true时 会自动加上 <script> 开始和结束标记
ClientScript.RegisterStartupScript 这个方法输出的js脚本在 </form>之前
你一开始用的Literal.Text来输出js脚本 也是可以的,它的位置相对灵活一些,
Literal放在那里就在那里输出js脚本,但Literal 只能放在 <form runat="server"> </form>中间
也就是输出的js脚本都在 </form>之前 也等于在 </body>之前
Internet Explorer cannot open the Internet site http://localhost:1463/Divtest/Default.aspx.
Opertation aborted
这个错误就是
MSDN:子容器 HTML 元素包含试图修改子容器的父容器元素的脚本.
上面是MSDN 的说法,在你的代码里 就是在body 闭合之前 不能修改它的内容
之前 <input type="button" value="点击这里" onclick="sAlert('渐变的',this);" />
好用 是因为你在单击按钮的时候 页面已经加载完毕了。
可ClientScript.RegisterStartupScript 不好用是
脚本加载到 </form>之前 </body>还没有出现,这个时候你调用了sAlert 方法
也就是调用了
document.body.appendChild(alertFram); document.body.appendChild(shield);
这在IE6,IE7中认为是不合法的,是错误的。 在IE8中是可以的。
所以出现了
Internet Explorer cannot open the Internet site http://localhost:1463/Divtest/Default.aspx.
Opertation aborted
解决办法 就是要不在 </body> 标记的后面加载js脚本,要不就是等页面加载之后运行sAlert 方法
所以就有了
window.attachEvent('onload', methodName);
这个形式 用于将sAlert 方法包装一下,在页面加载结束后运行