结合MS AJAX将js文件编译到动态链接库

为了使javascript代码不被窃取,我们可以将js文件编译成动态链接库(dll)文件。下面为了演示这一功能,创建了一个控件。

 程序代码:SampleControlsCS.rar

一、创建一个类库项目,命名为UpdateAnimate

二、向项目中添加引System.Web, System.Drawing, System.Web.Extensions

三、向项目中添加一个Jscript的文件UpdatePanelAnimation.js

四、向文件中添加如下代码:


BorderAnimation 
= function(color) 
{
    
this._color = color;
}


BorderAnimation.prototype 
= 
{
    animate: function(panelElement) 
    
{
        var s 
= panelElement.style;
        s.borderWidth 
= '2px';
        s.borderColor 
= this._color;
        s.borderStyle 
= 'solid';

        window.setTimeout(
            function() 
            
{
            
{
                s.borderWidth 
= 0;
            }

            }
,
            
500);
    }

}


这段代码中,包含一段临时改变UpdatePanel控件样式的方法

 

五、解决方案资源管理器中,右键查看UpdatePanelAnimation.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。

六、向项目中添加一个类CustomControl

七、替换类中的代码:

using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace UpdateAnimate
{
    
public class UpdatePanelAnimationWithClientResource : Control
    
{
        
private string _updatePanelID;
        
private Color _borderColor;
        
private Boolean _animate;
        
public Color BorderColor
        
{
            
get
            
{
                
return _borderColor;
            }

            
set
            
{
                _borderColor 
= value;
            }

        }


        
public string UpdatePanelID
        
{
            
get
            
{
                
return _updatePanelID;
            }

            
set
            
{
                _updatePanelID 
= value;
            }

        }


        
public Boolean Animate
        
{
            
get
            
{
                
return _animate;
            }

            
set
            
{
                _animate 
= value;
            }

        }

        
protected override void OnPreRender(EventArgs e)
        
{
            
base.OnPreRender(e);
            
if (Animate)
            
{

                UpdatePanel updatePanel 
= (UpdatePanel)FindControl(UpdatePanelID);

                
string script = String.Format(
                   CultureInfo.InvariantCulture,
                   
@"
Sys.Application.add_load(function(sender, args) {{
var {0}_borderAnimation = new BorderAnimation('{1}');    
var panelElement = document.getElementById('{0}');
     if (args.get_isPartialLoad()) {{
        {0}_borderAnimation.animate(panelElement);
    }}
}})
",
                   updatePanel.ClientID,
                   ColorTranslator.ToHtml(BorderColor));


                ScriptManager.RegisterStartupScript(
                    
this,
                    
typeof(UpdatePanelAnimationWithClientResource),
                    ClientID,
                    script,
                    
true);
            }

        }

    }

}



八、AssemblyInfo.cs文件中添加如下行:

[assembly: System.Web.UI.WebResource("UpdateAnimate.UpdatePanelAnimation.js", "application/x-javascript")]

九、生成项目。

 

控件演示:

一、创建一个AJAX-enabled类型的网站项目。

二、向网站跟目录下添加bin目录。

三、从控件项目的bin\Debug bin\Release目录拷贝UpdateAnimate.dll到网站bin目录里。

四、替换Default.aspx的内容并运行程序:

 

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register TagPrefix="Samples" Namespace="UpdateAnimate" Assembly="UpdateAnimate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    
<title>ScriptReference</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering
="True"
                                 runat
="server">
             
<Scripts>
                
<asp:ScriptReference Assembly="UpdateAnimate" Name="UpdateAnimate.UpdatePanelAnimation.js" />
             
</Scripts>
            
</asp:ScriptManager>


            
<Samples:UpdatePanelAnimationWithClientResource 
                     ID
="UpdatePanelAnimator1"
                     BorderColor
="Green"
                     Animate
="true"
                     UpdatePanelID
="UpdatePanel1"
                     runat
="server" >
            
</Samples:UpdatePanelAnimationWithClientResource>
            
<asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode
="Conditional"
                               runat
="server">
                
<ContentTemplate>
                    
<asp:Calendar ID="Calendar2" 
                                  runat
="server">
                    
</asp:Calendar>
                
</ContentTemplate>
            
</asp:UpdatePanel>
        
</div>
    
</form>
</body>
</html>

 

posted on 2007-02-01 18:16 ☆聊ゾ聊☆ 阅读(2288) 评论(11) 编辑 收藏

评论

#1楼  回复 引用 查看   

抢个沙发,再看
2007-02-01 18:19 | jailu      

#2楼  回复 引用 查看   

不输出到页面?
2007-02-01 19:56 | 亚历山大同志      

#3楼  回复 引用   

"为了使javascript代码不被窃取",
在http协议下,没有什么不可以被窃取的。。
2007-02-01 20:57 | shewo[未注册用户]

#4楼  回复 引用 查看   

内嵌JavaScript估计只是为了部署方便,和窃取不窃取无关。
2007-02-01 21:39 | Jeffrey Zhao      

#5楼  回复 引用 查看   

任何浏览器直接打开的东西都已经在客户端缓缓存,如何能不被窃取?
2007-02-01 22:49 | Cat Chen      

#6楼  回复 引用   

这不过是鸵鸟把头插到土里面.原谅我说得这么直. 先不论浏览器会缓存脚本,就算是直接去截获数据流也能轻易得到. 最有效的办法就是不要将任何敏感的数据发到客户端.
2007-02-02 08:59 | ivan[匿名][未注册用户]

#7楼  回复 引用   

我使用任何http监听工具即可查看源代码。不管你再怎么加密
2007-02-02 09:10 | try[匿名][未注册用户]

#8楼  回复 引用   

明显不了解web和javascript的原理
2007-02-02 09:33 | kevin[未注册用户]

#9楼  回复 引用   

你要知道,你这么做,会造成页面输出流变大,
如果JS很多,可能会导致响应速度变慢,我觉得把脚本通过URL来重新定向,这样可能会比较好
窃取,楼上的兄弟们已经说了很多,我就不说了
不过可以混淆,但是我觉得没多大的意思
2007-02-02 12:44 | cnblogs-yi[未注册用户]

#10楼  回复 引用 查看   

还是有很多东西值得学习的嘛,楼主不用担心人家给你指出缺点,有不足才有进步嘛!
2007-02-02 16:43 | today      

#11楼  回复 引用   

我想写个控件 !必须调用一个js文件里的 方法... 但是我又不想去把这些方法 拼成字符串... 各位哥们不知道有什么好的办法...
可以的话 麻烦给我留言 博客地址: www.cnblogs.com/RevengeBoy
2007-07-06 14:51 | RevengeBoy[未注册用户]

导航

<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

公告

昵称:☆聊ゾ聊☆
园龄:5年
粉丝:3
关注:0

搜索

 
 

常用链接

随笔档案

相册

最新评论

阅读排行榜

评论排行榜

推荐排行榜