// 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 方法
也就是調用了
-
JScript code
-
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 方法封裝一下,在頁面載入結束後運行