.NET Web开发后台常用JS方法分享

一、前言

JavaScript是一种动态的、轻量级的计算机编程语言,而我们在Web后台开发过程中经常会遇到后台方法很难实现而JS却能轻松解决的场景,使用JavaScript的优点如下:

  • 更少的服务器交互作用-您可以在将页面发送给服务器之前验证用户输入。这样可以节省服务器流量,这意味着服务器上的负载更少。

  • 立即向访问者反馈-他们不必等待页面重新加载就可以查看是否忘记了输入内容。

  • 增强的交互性-您可以创建界面,当用户将鼠标悬停在界面上或通过键盘激活界面时会做出反应。

  • 丰富的界面-您可以使用JavaScript包含诸如拖放组件和滑块之类的项目,以向网站访问者提供丰富的界面。

二、目录

这里分享几个工作中经常会用到的后台调用JS的方法:

1、自定义弹出信息 

2、弹出信息,并跳转指定页面

3、弹出信息,并返回历史页面

4、回到历史页面

5、关闭当前窗口

6、刷新父窗口

7、返回到父窗口

8、页面跳转(跳出框架)

三、具体实现如下

1、自定义弹出信息

public static void Alert(string message)
{
    string js = "<script language=javascript>alert('{0}');</script>";
    HttpContext.Current.Response.Write(string.Format(js, message));
}

 2、弹出信息,并跳转指定页面

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));
    HttpContext.Current.Response.End();
}

 3、弹出信息,并返回历史页面

public static void GoHistory(string message, int value)
{
    string js = @"<Script language='JavaScript'>alert('{0}');history.go({1});</Script>";
    HttpContext.Current.Response.Write(string.Format(js, message, value));
    HttpContext.Current.Response.End();
}

 4、回到历史页面

public static void GoHistory(int value)
{

    string js = @"<Script language='JavaScript'> history.go({0});  </Script>";
    HttpContext.Current.Response.Write(string.Format(js, value));
}

 5、关闭当前窗口

public static void CloseWindow()
{
    string js = @"<Script language='JavaScript'>parent.opener=null;window.close();</Script>";
    HttpContext.Current.Response.Write(js);
    HttpContext.Current.Response.End();  
}

 6、刷新父窗口

public static void RefreshParent(string url)
{
    string js = @"<script>try{top.location=""" + url + @"""}catch(e){location=""" + url + @"""}</script>";
    HttpContext.Current.Response.Write(js);
}

 7、返回到父窗口

public static void ParentRedirect(string ToUrl)
{
    string js = "<script language=javascript>window.top.location.replace('{0}')</script>";
    HttpContext.Current.Response.Write(string.Format(js, ToUrl));
}

 8、页面跳转(跳出框架)

public static void JavaScriptExitIfream(string url)
{
    string js = @"<Script language='JavaScript'> parent.window.location.replace('{0}'); </Script>";
    js = string.Format(js, url);
    HttpContext.Current.Response.Write(js);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值