Scriptmanager删除javascript [英] Scriptmanager remove javascript

查看:64
本文介绍了Scriptmanager删除javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET ScriptManager控件自动插入各种内联JavaScript,例如PageRequest initialize.是否可以将其删除或移动到外部js文件?

The ASP.NET ScriptManager control automatically inserts all kinds of inline javascript like PageRequest initialize. Is it possible to remove or move this to an external js file?

此外,即使在页面上未使用脚本管理器时,总是会添加__DoPostback,如何避免这种情况或将其移至外部文件?

Also, the scriptmanager always adds __DoPostback even when not used on the page, how can this be avoided or also moved to an external file?

推荐答案

首先,控件会插入"__DoPostBack",这些控件会导致回发,例如DropDownList且AutoPostBack属性设置为true.由于ScriptManager基本上可以拦截传统的回发,所以我相信这就是它插入函数的原因.

First of all the "__DoPostBack" is inserted by controls that can cause a postback such as the DropDownList with the AutoPostBack property set to true. Since the ScriptManager basically intercepts the traditional postbacks, I believe that's the reason for it to insert the function.

-以下只是我的想法...:)

-- The following is just me thinking... :)

现在,关于将所有内容移至外部文件.这并不容易,但是可能"是可能的.问题是ASP.NET在运行时生成脚本,因此您不能静态地对其进行任何操作.您需要考虑的是为什么首先要这样做.事实是,大多数生成的脚本是动态的,因此很难缓存.但是,如果确实需要,您应该同时查看HttpHandlers和HttpModules.

Now, about moving everything to an external file. It's not easy, but it "could" be possible. The problem is that ASP.NET is generating the scripts at runtime, so you cannot do anything about it statically. What you need to consider is why you would want to do this in the first place. The fact is that much of the generated script is dynamic, which makes it rather hard to cache. But, if you really need to, you should have a look at both HttpHandlers and HttpModules.

基本上,您需要以某种方式提取每个脚本标签(不设置src属性).这可以在HttpContext的BeginRequest事件上的HttpModule中完成.现在,您需要提取所有必要的信息,并使用对特定HttpHandler的引用来替换它,该HttpHandler可以用作替换内容.但是,要使它们完全不同,有必要对现有脚本进行某种形式的缓存.您可能为此使用ASP.NET缓存.棘手的部分是将现有缓存条目与新缓存条目进行比较,并确定是否获取缓存版本(将HttpHandler指向现有条目)或生成新条目.如果您有很多脚本,那么它很可能是一个相当昂贵的操作.此外,您需要确定客户端是否可以自行缓存(电子标签等可能会派上用场).对于客户端而言,重要的是避免每次都下载相同的不必要脚本(我相信这是您的最终目标?).

Basically you need to somehow extract every script tag (without the src-attribute set). This could be done in a HttpModule on the BeginRequest event of the HttpContext. Now you need to extract all the necessary pieces of information and replace it with a reference to a specific HttpHandler that can service as the replacement. But to make any difference at all, it is necessary for you to do some sort of caching of the existing script. You could probably use the ASP.NET Cache for that. The tricky part would be to compare an existing cache entry with the new and determine whether or not to get the cached version (pointing the HttpHandler to an existing entry) or to generate a new entry. If you have a lot of scripts, it's most likely to be a rather expensive operation. Furthermore you need to determine whether the client can cache on it's own (e-tags etc. could come in handy). What's important is for the client to avoid downloading the same unnecessary scripts each time (I believe that's your ultimate goal?).

所以回顾一下:

  • 构建一个HttpModule来处理页面重写,并将提取的脚本放入某种缓存中(例如ASP.NET缓存).
  • 构建一个HttpHandler指向提取的脚本(它应该从缓存中流式传输内容).处理程序应代替提取的内联脚本.
  • 创建某种算法来确定缓存无效性.我不知道怎么在请求之间更改哪种脚本.

顺便说一句.脚本外部化最多是棘手的,因此您需要注意不要引入无法修复的错误;)

Btw. script externalization is tricky at most, so you need to be careful not to introduce bugs that are impossible to fix ;)

这不是一个精确的解决方案,但是我之前没有做任何运气就尝试过这样做……主要是因为时间太少了.当您付钱去做某项任务时,这种优化是不合理的...:(

Not an extact solution, but I have tried doing this myself before without any luck... mostly because of too litle time. When you are payed to do a task, this kind of optimization can't be justified... :(

这篇关于Scriptmanager删除javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆