using System;
using System.Text;
using System.Data;
using System.Configuration;
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;
namespace JScript
{
/**//// <summary>
/// 提供向頁面輸出用戶端代碼實作特殊功能的方法
/// </summary>
/// <remarks>
/// </remarks>
public class JScript
/**//// <summary>
/// 加入收藏夾
/// </summary>
/// <param name="url">收藏網址</param>
/// <param name="urlname">收藏網址提示</param>
/// <param name="name">收藏網址名字</param>
public static void aa(string url,string urlname,string name)
{
string js = @"<a href=# onClick=window.external.addFavorite(‘"+url+",‘"+urlname+"‘) target=_self title=‘"+urlname+"‘>"+name+"</a> ";
HttpContext.Current.Response.Write(js);
}
/// 加載以後寫自己的腳本
/// <param name="pages"></param>
/// <param name="yourJs"></param>
public static void ClientWrite(string yourJs)
Page pages;
pages = HttpContext.Current.Handler as System.Web.UI.Page;
pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>" + yourJs + "</script>"));
/**//**/
/// 加載以前寫自己的腳本
public static void ClientWrite2(string yourJs)
pages.Response.Write("<script language=javascript>");
pages.Response.Write(yourJs);
pages.Response.Write(" </script>");
/// 窗體加載以後探出對話框
/// </summary>
public static void Alert(string msg)
msg = msg.Replace("‘", "");
msg = msg.Replace("\"", "");
msg = msg.Replace("\n", @"\n").Replace("\r", @"\r").Replace("\"", @"\""");
pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>alert(‘" + msg + "‘);</script>"));
/// 窗體沒有加載的時候如pageload的時候探出對話框
public static void Alert_none(string msg)
string retu = " alert(‘" + msg + "‘);";
ClientWrite2(retu);
public static void AlertAndRedirect(string message, string toURL)
string js = "<script language=javascript>alert(‘{0}‘);window.location.replace(‘{1}‘)</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
/// 彈出JavaScript小視窗
/// <param name="js">視窗資訊</param>
public static void Alert(object message)
string js = @"<Script language=‘JavaScript‘>
alert(‘{0}‘);
</Script>";
HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
public static void RtnRltMsgbox(object message, string strWinCtrl)
strWinCtrl = true;
strWinCtrl = if(!confirm(‘" + message + "‘))return false;</Script>";
/// 回到曆史頁面
/// <param name="value">-1/1</param>
public static void GoHistory(int value)
history.go({0});
HttpContext.Current.Response.Write(string.Format(js, value));
/// 關閉目前視窗
public static void CloseWindow()
window.close();
HttpContext.Current.Response.End();
/// 重新整理本頁
public static void Refreshself()
window.location=window.location.href;
/// 重新整理父視窗
public static void RefreshParent(string parurl)
window.opener.location.reload(‘" + parurl + "‘)</Script>";
/// 重新整理父架構
public static void RefreshParentifarme(string url)
window.parent.location.reload(‘"+url+"‘)</Script>";
/// 格式化為JS可解釋的字元串
/// <param name="s"></param>
/// <returns></returns>
public static string JSStringFormat(string s)
return s.Replace("\r", "\\r").Replace("\n", "\\n").Replace("‘", "\\‘").Replace("\"", "\\\"");
/// 重新整理打開視窗
public static void RefreshOpener()
opener.location.reload();
/// 打開小窗體
/// <param name="url"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="top"></param>
/// <param name="left"></param>
public static void OpenLittleWindow(string url, int width, int height, int top, int left)
string str,js;
str = "var popup;popup=window.open(‘{url}‘,null,‘scrollbars=yes,status=no,width={width},height={height},top={top},left={left}‘);popup.opener=self.opener;self.close();";
str = str.Replace("{width}", width.ToString());
str = str.Replace("{height}", height.ToString());
str = str.Replace("{top}", top.ToString());
str = str.Replace("{left}", left.ToString());
str = str.Replace("{url}", url);
js = @"<Script language=‘JavaScript‘>"+str+" </Script>";
/// 打開視窗
public static void OpenWebForm(string url)
/**//*…………………………………………………………………………………………*/
/**//*修改目的: 新開頁面去掉ie的菜單。。。 */
//window.open(‘" + url + @"‘);
window.open(‘" + url + @"‘,‘‘,‘height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no‘);
</Script>";
/**//*結束*/
public static void OpenWebForm(string url, string name, string future)
window.open(‘" + url + @"‘,‘" + name + @"‘,‘" + future + @"‘)
public static void OpenWebForm(string url, string formName)
/**//*注釋内容: */
/**//*開始*/
window.open(‘" + url + @"‘,‘" + formName + @"‘,‘height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no‘);
/**//// <summary>
/// 函數名:OpenWebForm
/// 功能描述:打開WEB視窗
/// <param name="url">WEB視窗</param>
/// <param name="isFullScreen">是否全螢幕</param>
public static void OpenWebForm(string url, bool isFullScreen)
string js = @"<Script language=‘JavaScript‘>";
if (isFullScreen)
js += "var iWidth = 0;";
js += "var iHeight = 0;";
js += "iWidth=window.screen.availWidth-10;";
js += "iHeight=window.screen.availHeight-50;";
js += "var szFeatures =‘width=‘ + iWidth + ‘,height=‘ + iHeight + ‘,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no‘;";
js += "window.open(‘" + url + @"‘,‘‘,szFeatures);";
}
else
js += "window.open(‘" + url + @"‘,‘‘,‘height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no‘);";
js += "</Script>";
/// 轉向Url制定的頁面
public static void JavaScriptLocationHref(string url)
window.location.replace(‘{0}‘);
js = string.Format(js, url);
/// 指定的架構頁面轉換
/// <param name="FrameName"></param>
public static void JavaScriptFrameHref(string FrameName, string url)
@obj.location.replace(""{0}"");
js = js.Replace("@obj", FrameName);
///重置頁面
public static void JavaScriptResetPage(string strRows)
window.parent.CenterFrame.rows=‘" + strRows + "‘;</Script>";
/// 函數名:JavaScriptSetCookie
/// 功能描述:用戶端方法設定Cookie
/// <param name="strName">Cookie名</param>
/// <param name="strValue">Cookie值</param>
public static void JavaScriptSetCookie(string strName, string strValue)
string js = @"<script language=Javascript>
var the_cookie = ‘" + strName + "=" + strValue + @"‘
var dateexpire = ‘Tuesday, 01-Dec-2020 12:00:00 GMT‘;
//document.cookie = the_cookie;//寫入Cookie<BR>} <BR>
document.cookie = the_cookie + ‘; expires=‘+dateexpire;
</script>";
/// 函數名:GotoParentWindow
/// 功能描述:傳回父視窗
/// <param name="parentWindowUrl">父視窗</param>
public static void GotoParentWindow(string parentWindowUrl)
this.parent.location.replace(‘" + parentWindowUrl + "‘);</Script>";
/// 函數名:ReplaceParentWindow
/// 功能描述:替換父視窗
/// <param name="parentWindowUrl">父視窗</param>
/// <param name="caption">視窗提示</param>
/// <param name="future">視窗特征參數</param>
public static void ReplaceParentWindow(string parentWindowUrl, string caption, string future)
string js = "";
if (future != null && future.Trim() != "")
js = @"<script language=javascript>this.parent.location.replace(‘" + parentWindowUrl + "‘,‘" + caption + "‘,‘" + future + "‘);</script>";
js = @"<script language=javascript>var iWidth = 0 ;var iHeight = 0 ;iWidth=window.screen.availWidth-10;iHeight=window.screen.availHeight-50;
var szFeatures = ‘dialogWidth:‘+iWidth+‘;dialogHeight:‘+iHeight+‘;dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes‘;this.parent.location.replace(‘" + parentWindowUrl + "‘,‘" + caption + "‘,szFeatures);</script>";
/// 函數名:ReplaceOpenerWindow
/// 功能描述:替換目前窗體的打開視窗
/// <param name="openerWindowUrl">目前窗體的打開視窗</param>
public static void ReplaceOpenerWindow(string openerWindowUrl)
window.opener.location.replace(‘" + openerWindowUrl + "‘);</Script>";
/// 函數名:ReplaceOpenerParentWindow
/// 功能描述:替換目前窗體的打開視窗的父視窗
/// <param name="openerWindowUrl">目前窗體的打開視窗的父視窗</param>
public static void ReplaceOpenerParentFrame(string frameName, string frameWindowUrl)
window.opener.parent." + frameName + ".location.replace(‘" + frameWindowUrl + "‘);</Script>";
public static void ReplaceOpenerParentWindow(string openerParentWindowUrl)
window.opener.parent.location.replace(‘" + openerParentWindowUrl + "‘);</Script>";
/// 函數名:CloseParentWindow
/// 功能描述:關閉視窗
public static void CloseParentWindow()
window.parent.close();
public static void CloseOpenerWindow()
window.opener.close();
/// 函數名:ShowModalDialogJavascript
/// 功能描述:傳回打開模式視窗的腳本
/// <param name="webFormUrl"></param>
public static string ShowModalDialogJavascript(string webFormUrl)
string js = @"<script language=javascript>
var iWidth = 0 ;
var iHeight = 0 ;
iWidth=window.screen.availWidth-10;
iHeight=window.screen.availHeight-50;
var szFeatures = ‘dialogWidth:‘+iWidth+‘;dialogHeight:‘+iHeight+‘;dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes‘;
showModalDialog(‘" + webFormUrl + "‘,‘‘,szFeatures);</script>";
return js;
public static string ShowModalDialogJavascript(string webFormUrl, string features)
string js = @"<script language=javascript>
showModalDialog(‘" + webFormUrl + "‘,‘‘,‘" + features + "‘);</script>";
/// 函數名:ShowModalDialogWindow
/// 功能描述:打開模式視窗
public static void ShowModalDialogWindow(string webFormUrl)
string js = ShowModalDialogJavascript(webFormUrl);
public static void ShowModalDialogWindow(string webFormUrl, string features)
string js = ShowModalDialogJavascript(webFormUrl, features);
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
string features = "dialogWidth:" + width.ToString() + "px"
+ ";dialogHeight:" + height.ToString() + "px"
+ ";dialogLeft:" + left.ToString() + "px"
+ ";dialogTop:" + top.ToString() + "px"
+ ";center:yes;help=no;resizable:no;status:no;scroll=no";
ShowModalDialogWindow(webFormUrl, features);
public static void SetHtmlElementValue(string formName, string elementName, string elementValue)
string js = @"<Script language=‘JavaScript‘>if(document." + formName + "." + elementName + "!=null){document." + formName + "." + elementName + ".value =" + elementValue + ";}</Script>";
/**//**/
/// 回車-〉tab
/// <param name="page"></param>
public static void ToTab()
Page page;
page = HttpContext.Current.Handler as System.Web.UI.Page;
System.Text.StringBuilder scriptFunction = new StringBuilder();
scriptFunction.Append("<script language=‘javascript‘>");
scriptFunction.Append(" function returnTotab()");
scriptFunction.Append(" {");
scriptFunction.Append(" if(event.keyCode==13) ");
scriptFunction.Append(" {event.keyCode=9; ");
scriptFunction.Append(" return true;} ");
scriptFunction.Append(" } ");
scriptFunction.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(),"totab", scriptFunction.ToString());
}
/**//**/
/// tab->enter
public static void tabToEnter()
scriptFunction.Append(" function Tcheck()");
scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
scriptFunction.Append(" return true;");
scriptFunction.Append(" else ");
scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
scriptFunction.Append(" return true;");
scriptFunction.Append(" else");
scriptFunction.Append(" if(event.keyCode==13||event.keyCode==110||event.keyCode==190||event.keyCode==39)");
scriptFunction.Append(" {event.keyCode=9;");
scriptFunction.Append(" return true;}");
scriptFunction.Append(" else");
scriptFunction.Append(" return false;");
scriptFunction.Append(" }");
scriptFunction.Append(" } ");
page.ClientScript.RegisterStartupScript(page.GetType(),"switch", scriptFunction.ToString());
/// attachEvent
/// <param name="controlToFocus"></param>
public static void attachEvent(Control[] controlToFocus)
string scriptClientId;
foreach (Control con in controlToFocus)
scriptClientId = con.ClientID;
scriptFunction.Append("document.getElementById(‘" + scriptClientId + "‘).attachEvent(‘onkeydown‘, Tcheck);");
page.ClientScript.RegisterStartupScript(page.GetType(),"attach", scriptFunction.ToString());
///
/// <param name="eventStr"></param>
/// <param name="FuncStr"></param>
public static void AttachEvent(Control[] controlToFocus, string
eventStr, string FuncStr)
scriptFunction.Append("document.getElementById(‘" + scriptClientId + "‘).attachEvent(‘" + eventStr + "‘, " + FuncStr + ");");
page.ClientScript.RegisterStartupScript(page.GetType(),"attach2", scriptFunction.ToString());
public static void NumOnlyFun()
Page page;
page = HttpContext.Current.Handler as System.Web.UI.Page;
System.Text.StringBuilder scriptFunction = new StringBuilder();
scriptFunction.Append("<script language=‘javascript‘>");
scriptFunction.Append(" function isNum()");
scriptFunction.Append(" {");
scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
scriptFunction.Append(" return true;");
scriptFunction.Append(" else ");
scriptFunction.Append(" {");
scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
scriptFunction.Append(" return true;");
scriptFunction.Append(" else");
scriptFunction.Append(" return false;");
scriptFunction.Append(" }");
scriptFunction.Append(" } ");
scriptFunction.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "numonly", scriptFunction.ToString());
}