zoukankan      html  css  js  c++  java
  • ASP.NET提供了三种后台输出JS的方式

    ASP.NET提供了三种后台输出JS的方式:

      一、后台输出已有js文件

      首先创建 js文件testjs.js

    以下是代码片段:
     if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "keys"))//判断keys是否已注册过
      {
      Page.ClientScript.RegisterClientScriptInclude("keys", "testjs.js");
      }

      二、输出js代码块

    以下是代码片段:
     string scriptstrs = "";//此处只作为演示,如代码需多次拼接应采用StringBuilder方式
      scriptstrs += "function test(str)";
      scriptstrs+="{alert(str);}";
      if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "keys"))
      {
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "keys", scriptstrs, true);
      }

      三、 输出一次性使用的js代码

    以下是代码片段:
     string scriptstrs = "";
      if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),"welcome"))
      {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
      }

      此外,运用Response.Write(" "); 方式也可输出简单js代码,但我个人不提倡采用此种方式。因为在以前开发中遇到有些情况下此种方式会导致弹出提示信息后页面字号改变的现象,所以安全起见建议采用上述三种方式。

  • 相关阅读:
    事件(三):事件对象
    事件(二):事件处理程序
    事件(一):事件流
    nginx里面的rewrite配置
    详解 CSS 居中布局技巧
    jQuery 效率提升建议
    web的攻击技术
    前端优化点总结
    深入理解递归和闭包
    创建对象
  • 原文地址:https://www.cnblogs.com/5tao/p/2124789.html
Copyright © 2011-2022 走看看