JavaScript 相关 —— 向页面写JS以alert信息、通过对地址栏赋值转到指定页、注册Js到Body的开始部分、注册Js到Body的结束位置

#region JS相关
    /// <summary>
    /// 向页面写JS以alert信息
    /// </summary>
    /// <param name="msg">信息</param>
    protected void AlertMessage(string msg)
    {
        if (ScriptManager.GetCurrent(this) != null)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
                "alert('"
                + msg.Replace("/r/n", "
///r///n").Replace("'", "//'").Replace("/"", "///"")
                + "');", true);
        }
        else
        {
            Response.Write("<script language=javascript>alert('"
                + msg.Replace("/r/n", "
///r///n").Replace("'", "//'").Replace("/"", "///"")
                + "');</script>");
        }
    }

    /// <summary>
    /// 通过对地址栏赋值转到指定页
    /// </summary>
    /// <param name="pagePath">相对本页的指定页路径</param>
    protected void PageGo(string pagePath)
    {
        if (ScriptManager.GetCurrent(this) != null)
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "window.location.href='" +

pagePath + "';", true);
        else
            Response.Write("<script language=javascript>window.location.href='" + pagePath + "';</script>");
    }
    /// <summary>
    /// 通过 javascript:window.close(); 关闭页
    /// </summary>
    protected void PageClose()
    {
        if (ScriptManager.GetCurrent(this) != null)
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "window.close();", true);
        else
            Response.Write("<script language=javascript>window.close();</script>");
    }

    /// <summary>
    /// 注册Js到Body的开始部分
    /// </summary>
    /// <param name="jsContent">js内容,不需要 script ...</param>
    protected void RegisterJSToBodyStart(string jsContent)
    {
        if (ScriptManager.GetCurrent(this) != null)
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), jsContent, true);
        else
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), jsContent, true);
    }
    /// <summary>
    /// 注册Js到Body的结束位置
    /// </summary>
    /// <param name="jsContent">js内容,不需要 script ...</param>
    protected void RegisterJSToBodyEnd(string jsContent)
    {
        if (ScriptManager.GetCurrent(this) != null)
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), jsContent, true);
        else
            this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), jsContent, true);
    }
#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值