欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

常用JavaScript代码提示公共类封装

程序员文章站 2024-03-08 23:10:58
希望对大家有帮助!复制代码 代码如下:using system; using system.web; namespace jake.publicjs { ///
希望对大家有帮助!
复制代码 代码如下:

using system;
using system.web;
namespace jake.publicjs
{

/// <summary>
/// summary description for publicjs
/// </summary>
public class publicjs
{
public publicjs()
{
//
// todo: add constructor logic here
//
}

/// <summary>
/// 1.静态方法,弹出信息窗体
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="description">信息内容</param>
/// <example>
/// publicjs.alert(this,"nihao!");
/// </example>
public static void alert(system.web.ui.page page, string description)
{
if (description != null)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "alert('" + description + "');";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}
else
{
alert(page, "描述信息为空!");
}
}

/// <summary>
/// 2.静态方法,弹出信息窗体,并刷新页面
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="description">信息内容</param>
/// <example>
/// publicjs.alert(this,"nihao!");
/// </example>
public static void reloadmessage(system.web.ui.page page, string description, string pageid)
{
if (description != null)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "alert('" + description + "');";
scriptstring += "parent." + pageid + ".location.reload()";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}
else
{
alert(page, "描述信息为空!");
}
}
public static void redirect(string url)
{
httpresponse response;
response = httpcontext.current.response;
response.write("<script language=jscript><!--
\n");
response.write("window.location='" + url + "';\n");
response.write("
// --></script>\n");
}

/// <summary>
/// 弹出对话框,转向所指页面
/// </summary>
/// <param name="description">提示信息</param>
/// <param name="url">页面</param>
public static void msgboxredrict(string description, string url)
{
httpresponse response;
response = httpcontext.current.response;
response.write("<script language=jscript><!--
\n");
response.write("alert('" + description + "');\n");
response.write("window.location='" + url + "';\n");
response.write("
// --></script>\n");
//response.redirect(url);
}

/// <summary>
/// 弹出对话框,确实转向所指页面
/// </summary>
/// <param name="description">提示信息</param>
/// <param name="url">页面</param>
/// <param name="printurl">确定后转向的页面</param>
public static void msgboxredrict(string description, string url, string printurl)
{
httpresponse response;
response = httpcontext.current.response;
response.write("<script language=jscript><!--
\n");
response.write("function prints()\n");
response.write("{\n if(confirm('" + description + "'))\n");
response.write("{window.location='" + printurl + "';}\n");
response.write("else\n");
response.write("{window.location='" + url + "';}\n}\n");
response.write("prints();\n");
response.write("
// --></script>\n");
}

/// <summary>
/// 弹出对话框,转向所指页面
/// </summary>
/// <param name="description">提示信息</param>
public static void msgboxredrict(string description)
{
httpresponse response;
response = httpcontext.current.response;
response.write("<script language=jscript><!--
\n");
response.write("alert('" + description + "');\n");
response.write("history.go(-1);\n");
response.write("
// --></script>\n");
}
/// <summary>
///2 静态方法,关闭一个网页的父窗口,例如一个frame关闭其父窗口。
/// </summary>
/// <param name="page">页面对象</param>
/// <example>
/// publicjs.closeparent(this);
/// </example>
public static void closeparent(system.web.ui.page page)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "window.parent.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
///2 静态方法,关闭一个网页窗口。
/// </summary>
/// <param name="page">页面对象</param>
/// <example>
/// publicjs.closeparent(this);
/// </example>
public static void closepage(system.web.ui.page page)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "window.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
///3 静态方法,输出一则消息后关闭一个模态网页窗口并刷新父窗口
/// 前提条件是必须调用此类中的openmodaldialog方法
/// 在该方法中自动生成刷新方法才能实现父页面刷新。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="page">输出消息</param>
/// <example>
/// publicjs.closemodaldialog(this);
/// </example>
public static void closemodaldialogmessage(system.web.ui.page page, string message)
{

string scriptstring = "<script language=javascript><!--
";
scriptstring += "alert('" + message + "');";
scriptstring += "window.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
///3 静态方法,关闭一个模态网页窗口并刷新父窗口
/// 前提条件是必须调用此类中的openmodaldialog方法
/// 在该方法中自动生成刷新方法才能实现父页面刷新。
/// </summary>
/// <param name="page">页面对象</param>
/// <example>
/// publicjs.closemodaldialog(this);
/// </example>
public static void closemodaldialog(system.web.ui.page page)
{

string scriptstring = "<script language=javascript><!--
";
scriptstring += "window.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
/// 关闭模态网页并传值到父页面
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="strvalue">需要传递的值</param>
public static void closemodaldialog(system.web.ui.page page, string strvalue)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "window.returnvalue='" + strvalue.trim() + "';";
scriptstring += "window.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
///4 静态方法,关闭一个网页窗口。
/// </summary>
/// <param name="page">页面对象</param>
/// <example>
/// publicjs.closewindow(this);
/// </example>
public static void closewindow(system.web.ui.page page)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += "window.opener=null;";
scriptstring += "window.close();";
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", scriptstring);
}

/// <summary>
///5 静态方法,执行客户端一小块脚本语言,
///利用page的registerclientscriptblock方法在客户端注册一段脚本,
///参数script无需包括html标记<script type="text/javascript"><!--

// --></script>。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="script">javascript脚本</param>
/// <example>
/// publicjs.executeblock(this,"alert("hello");");
/// </example>
public static void executeblock(system.web.ui.page page, string script)
{
if (script != null)
{
string scriptstring = "<script language=javascript><!--
";
scriptstring += script;
scriptstring += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript9"))
page.registerclientscriptblock("clientscript9", scriptstring);
}
else
{
alert(page, "javascript脚本不能为空!");
}
}


/// <summary>
///6    静态方法,打开一个网页对话框,并生成刷新页面方法。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <example>
/// publicjs.openmodaldialog(page,"weihu.aspx",700,350);
/// </example>
public static void openmodaldialog(system.web.ui.page page, string url, int width, int height)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string scriptstring = "<script language='javascript'><!--
";
scriptstring += "function refreshs()";
scriptstring += "{";
//scriptstring += "window.location.href= window.location.href;";
scriptstring += "}";
scriptstring += "window.showmodaldialog('" + url + "',window,'dialogheight:" + height + "px;dialogwidth:" + width + "px;center:yes;help:no;scroll:auto;resizable:no;status:no;');";
scriptstring += "window.location.href= window.location.href;";

scriptstring += "
// --></script>";
if (!page.isstartupscriptregistered("startup"))
page.registerstartupscript("startup", scriptstring);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///7    静态方法,打开一个模式对话框
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="attribute">属性</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <example>
/// publicjs.openmodaldialog(page,"weihu.aspx","scrollbars=yes,status=yes",700,350);
/// </example>
public static void openmodaldialog(system.web.ui.page page, string url, string attribute, int width, int height)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
if (attribute == "")
attribute = "center:yes;help:no;scroll:no;resizable:no;status:no;";
string scriptstring = "<script language='javascript'><!--
";
//scriptstring += "function refresh()";
//scriptstring += "{";                    
scriptstring += "window.showmodaldialog('" + url + "',window,'dialogheight:" + height + "px;dialogwidth:" + width + "px;" + attribute + "')";
//scriptstring += "    window.location.href= window.location.href;";    
//scriptstring += "}";
//scriptstring += " refresh();";
scriptstring += "
// --></script>";
if (!page.isstartupscriptregistered("startup"))
page.registerstartupscript("startup", scriptstring);
}
else
{
alert(page, "页面地址不能为空!");
}
}


/// <summary>
///8 静态方法,打开一个无模式网页对话框。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <example>
/// publicjs.opendialog(page,"weihu.aspx",700,350);
/// </example>
public static void opendialog(system.web.ui.page page, string url, int width, int height)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string str = "<script language='javascript'><!--
"
+ "window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=auto,resizable=yes,width=" + width + ",height=" + height + "')"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///9 静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等)。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="left">左边距</param>
/// <param name="top">上边距</param>
/// <example>
/// publicjs.openiewindow(page,"weihu.aspx",700,350,10,20);
/// </example>
public static void openiewindow(system.web.ui.page page, string url, int width, int height, int left, int top)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string str = "<script language='javascript'><!--
"
+ "tt = window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "'); tt.focus();"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///9 静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等)。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="left">左边距</param>
/// <param name="top">上边距</param>
/// <example>
/// publicjs.openiewindow(page,"weihu.aspx",700,350,10,20);
/// </example>
public static void openiewindows(system.web.ui.page page, string url, int width, int height, int left, int top)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
//string str = "<script language='javascript'><!--
"
// + "window.open('" + url + "','','location=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "');"
// + "
// --></script>";
string str = "<script language='javascript'><!--
"
+ "tt = window.open('" + url + "','','location=no,status=no,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes,fullscreen=no'); tt.focus();"
+ "
// --></script>";

if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

public static void openiewindows(system.web.ui.page page, string url, int width, int height)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string str = "<script language='javascript'><!--
";
str += "a = window.open(\"" + url + "\", \"\", \"fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=" + width + ",height=" + height + "\", true);";
str += "a.focus();";
str += "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}


/// <summary>
///10 静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等)。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <example>
/// publicjs.openiewindow(page,"weihu.aspx");
/// </example>
public static void openiewindow(system.web.ui.page page, string url)
{
if (url != null)
{
string str = "<script language='javascript'><!--
"
+ "var cwin=window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=screen.availwidth-20,height=screen.availheight-20,left=10,top=10');"
+ "cwin.resizeto(screen.availwidth-20,screen.availheight-20);"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///11 静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等)。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <example>
/// publicjs.openiewindow(page,"weihu.aspx",700,350);
/// </example>
public static void openiewindow(system.web.ui.page page, string url, int width, int height)
{
if (url != null)
{
string str = "<script language='javascript'><!--
"
+ "var cwin=window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + width + ",height=" + height + ",left=10,top=10');"
+ "cwin.moveto((screen.availwidth-" + width + ")/2,(screen.availheight-" + height + ")/2);"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///12 静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等)
///在屏幕的最右边,上下满屏,宽度由参数指定。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <example>
/// publicjs.openiewindowright(page,"weihu.aspx",700);
/// </example>
public static void openiewindowright(system.web.ui.page page, string url, int width)
{
if (url != null)
{
if (width == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string str = "<script language='javascript'><!--
"
+ "newwindow=window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + width + ",height=document.height');"
+ "newwindow.moveto(screen.width-" + width + ",0);newwindow.resizeto(" + width + ",screen.height);"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
///13    静态方法,打开一个ie窗口(无标题栏、工具栏、地址栏等),在屏幕的最右边,上下位置在中间。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <example>
/// publicjs.openiewindowright(page,"weihu.aspx",700,350);
/// </example>
public static void openiewindowright(system.web.ui.page page, string url, int width, int height)
{
if (url != null)
{
if (width == 0 || height == 0)
{
alert(page, "页面宽度和高度不能为零!");
return;
}
string str = "<script language='javascript'><!--
"
+ "newwindow=window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + width + ",height=" + height + "');"
+ "newwindow.moveto(screen.width-" + width + ",(screen.height-" + height + ")/2);"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

/// <summary>
/// 设置控件焦点
/// </summary>
/// <param name="kongjianmc">控件字符串</param>
public static void shezhijd(system.web.ui.page page, string kongjianmc)
{
string jiaoben = "";
if (kongjianmc != "")
{
jiaoben = "var control;";
jiaoben += "control = document.getelementbyid('" + kongjianmc + "');";
jiaoben += "if (control!=null) ";
jiaoben += "{document.all['" + kongjianmc + "'].focus();}";
page.registerstartupscript("focus", "<script type="text/javascript"><!--
" + jiaoben + "
// --></script>");
}
}

/// <summary>
///14 静态方法,全屏打开一个ie窗口(无标题栏、工具栏、地址栏等)。
/// </summary>
/// <param name="page">页面对象</param>
/// <param name="url">页面名称</param>
/// <example>
/// publicjs.openiewindow(page,"weihu.aspx");
/// </example>
public static void openiewindowfill(system.web.ui.page page, string url)
{
if (url != null)
{
string str = "<script language='javascript'><!--
"
+ "var cwin=window.open('" + url + "','','location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=(screen.availwidth),height=(screen.availheight),left=10,top=10');"
//+ "alert(screen.availwidth);alert(screen.availheight); cwin.moveto(0,0);"
+ "
// --></script>";
if (!page.isclientscriptblockregistered("clientscript"))
page.registerclientscriptblock("clientscript", str);
}
else
{
alert(page, "页面地址不能为空!");
}
}

}
}