javascript如何封装方法?

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;

 

/// <summary>

/// Js脚本类

/// </summary>

public class JS

{

public JS()

{

}

 

    public static void Echo(string msg)

    {

        HttpContext.Current.Response.Write(msg);

    }

    public static void Die(string msg)

    {

        HttpContext.Current.Response.Write(msg);

        HttpContext.Current.Response.End();

    }

 

    /// <summary>

    /// 弹出对话框

    /// </summary>

    /// <param name="msg">弹出对话框的内容http://www.intocn.net/</param>

    public static void Alert(string msg)

    {

        HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");

    }

    /// <summary>

    /// 弹出对话框

    /// </summary>

    /// <param name="p">Page</param>

    /// <param name="msg">弹出对话框的内容</param>

    public static void Alert(Page p,string msg)

    {

        p.ClientScript.RegisterStartupScript(p.GetType(), "ajaxjs", "alert('" + msg + "');", true);

    }

    /// <summary>

    /// 执行js

    /// </summary>

    /// <param name="p">Page</param>

    /// <param name="js">要执行的js</param>

    public static void ExecuteJs(Page p, string js)

    {

        p.ClientScript.RegisterStartupScript(p.GetType(),"myjs",js,true);

    }

 

    /// <summary>

    /// 弹出对话框

    /// </summary>

    /// <param name="msg">弹出对话框的内容</param>

    public static string PrintAlert(string msg)

    {

        return "<script language=javascript>alert('" + msg + "');</script>";

    }

 

    /// <summary>

    /// 页面转向

    /// </summary>

    /// <param name="url">弹出对话框的内容</param>

    public static void Go(string url)

    {

        HttpContext.Current.Response.Write("<script language=javascript>window.location.href='" + url + "';</script>");

    }

 

    /// <summary>

    /// 弹出对话框并URL转向

    /// </summary>

    /// <param name="msg">弹出对话框的内容</param>

    /// <param name="url">转向地址,-1为后退</param>

    public static void AlertAndGo(string msg, string url)

    {

        if (url == "-1")

        {

            HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');history.go(-1);</script>");

        }

        else

        {

            HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location.href='" + url + "';</script>");

        }

        HttpContext.Current.Response.End();

    }

 

    /// <summary>

    /// 弹出对话框并URL转向

    /// </summary>

    /// <param name="p">Page</param>

    /// <param name="msg">弹出对话框的内容</param>

    /// <param name="url">转向地址,-1为后退</param>

    public static void AlertAndGo(Page p, string msg, string url)

    {

        if (url == "-1")

        {

            p.ClientScript.RegisterStartupScript(p.GetType(), "ajaxjs", "alert('" + msg + "');history.go(-1);", true);

        }

        else

        {

            p.ClientScript.RegisterStartupScript(p.GetType(), "ajaxjs", "alert('" + msg + "');window.location.href='" + url + "';", true);

        }

    }

 

    /// <summary>

    /// 关闭当前窗口

    /// </summary>

    public static void CloseWindow()

    {

        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=JAVASCRIPT>window.close();</SCRIPT>");

        System.Web.HttpContext.Current.Response.End();

    }

 

    /// <summary>

    ///  打开没有Toolbar的新窗口(静态)  

    /// </summary>

    /// <param name="str_Url">页面路径</param>

    /// <param name="int_Height">窗口高度</param>

    /// <param name="int_Width">窗口宽度</param>

    /// <param name="str_WebName">窗口名称</param>

    public static void OpenWindow(string str_Url,int int_Height,int int_Width,string str_WebName)

    {

       //构造JAVASCRIPT

       string str_JavaScript = @"<Script language='JavaScript'>window.open('"+str_Url+@"','"+str_WebName+@"','height="+ int_Height +@"','width="+ int_Width +@"','top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,directories=no');</Script>";

       System.Web.HttpContext.Current.Response.Write(str_JavaScript);  

    }

 

    /// <summary>

    /// 打开指定大小位置的模式对话框

    /// </summary>

    /// <param name="webFormUrl">连接地址</param>

    /// <param name="width"></param>

    /// <param name="height"></param>

    /// <param name="top">距离上位置</param>

    /// <param name="left">距离左位置</param>

    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=yes";

        ShowModalDialogWindow(webFormUrl, features);

    }

    public static void ShowModalDialogWindow(string webFormUrl, string features)

    {

        string js = ShowModalDialogJavascript(webFormUrl, features);

        HttpContext.Current.Response.Write(js);

    }

    public static string ShowModalDialogJavascript(string webFormUrl, string features)

    {

        string js = @"<script language=javascript>showModalDialog('" + webFormUrl + "','','" + features + "');</script>";

        return js;

    }

 

    /// <summary>

    /// 刷新父窗口

    /// </summary>

    public static void RefreshParent(string url)

    {

        string js = @"<Script language='JavaScript'>window.opener.location.href='" + url + "';window.close();</Script>";

        HttpContext.Current.Response.Write(js);

    }

 

    /// <summary>

    /// 刷新打开窗口

    /// </summary>

    public static void RefreshOpener()

    {

        string js = @"<Script language='JavaScript'>opener.location.reload();</Script>";

        HttpContext.Current.Response.Write(js);

    }

 

    /// <summary>

    /// 回到历史页面

    /// </summary>

    /// <param name="value">-1/1</param>

    public static void GoHistory(int value)

    {

        string js = @"<Script language='JavaScript'>history.go({0});</Script>";

        HttpContext.Current.Response.Write(string.Format(js, value));

    }

 

    /// <summary>

    /// 显示一段自定义的输出代码

    /// </summary>

    /// <param name="page">页面指针,一般为This</param>

    public static void ResponseScript(System.Web.UI.Page page, string script)

    {

        StringBuilder sb = new StringBuilder();

        sb.Append("<script language=\"javascript\"> \n");

        sb.Append(script.Trim());

        sb.Append("</script>");

        page.Response.Write(sb.ToString());

    }

 

    /// <summary>

    /// 调用客户端JavaScript函数

    /// </summary>

    /// <param name="page">页面指针,一般为This</param>

    /// <param name="scriptName">函数名,带参数,:FunA(1);</param>

    public static void CallClientScript(System.Web.UI.Page page, string scriptName)

    {

        String csname = "PopupScript";

        Type cstype = page.GetType();

        System.Web.UI.ClientScriptManager cs = page.ClientScript;

        if (!cs.IsStartupScriptRegistered(cstype, csname))

        {

            String cstext = scriptName;

            cs.RegisterStartupScript(cstype, csname, cstext, true);

        }

    }

 

    /// <summary>

    /// 控件点击 消息确认提示框

    /// </summary>

    /// <param name="page">当前页面指针,一般为this</param>

    /// <param name="message">提示信息</param>

    public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string message)

    {

        Control.Attributes.Add("onclick", "return confirm('" + message + "');");

    }

 

    /// <summary>

    /// 显示一个弹出窗口,重新加载当前页

    /// </summary>

    public static void ShowAndReLoad(string message)

    {

        StringBuilder sb = new StringBuilder();

        sb.Append("<script language=\"javascript\"> \n");

        sb.Append("alert(\"" + message.Trim() + "\"); \n");

        sb.Append("window.location.href=window.location.href;\n");

        sb.Append("</script>");

 

        System.Web.HttpContext.Current.Response.Write(sb.ToString());

    }

 

    /// <summary>

    /// 页面重载

    /// </summary>

    public static void ReLoad()

    {

        StringBuilder sb = new StringBuilder();

        sb.Append("<script language=\"javascript\"> \n");

        sb.Append("window.location.href=window.location.href;");

        sb.Append("</script>");

        System.Web.HttpContext.Current.Response.Write(sb.ToString());

 

    }

 

    /// <summary>

    /// 显示一个弹出窗口,并关闭当前页

    /// </summary>

    /// <param name="message"></param>

    public static void ShowAndClose(string message)

    {

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("<script language=\"javascript\">\n");

        sb.Append("alert(\"" + message.Trim() + "\"); \n");

        sb.Append("window.close();\n");

        sb.Append("</script>\n");

        System.Web.HttpContext.Current.Response.Write(sb.ToString());

    }

 

    /// <summary>

    /// 显示一个弹出窗口,并转向上一页

    /// </summary>

    /// <param name="message"></param>

    public static void ShowPre(string message)

    {

        StringBuilder sb = new StringBuilder();

        sb.Append("<script language=\"javascript\"> \n");

        sb.Append("alert(\"" + message.Trim() + "\"); \n");

        sb.Append("var p=document.referrer; \n");

        sb.Append("window.location.href=p;\n");

        sb.Append("</script>");

 

        System.Web.HttpContext.Current.Response.Write(sb.ToString());

    }

    /// <summary>

    /// 父窗口跳转

    /// </summary>

    /// <param name="url"></param>

    public static void ParentGo(string url)

    {

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("<script language=\"javascript\">\n");

        sb.Append("window.parent.location.href=\"" + url + "\"; \n");

        sb.Append("</script>\n");

        System.Web.HttpContext.Current.Response.Write(sb.ToString());

    }

 

}

 

你可能感兴趣的:(javascript如何封装方法?)