通过 UpdatePanel 异步回发后重新加载外部 javascript

发布于 2024-08-13 02:55:01 字数 583 浏览 3 评论 0原文

我的页面上有一个外部 javascript,例如:

<script src="http://foo.com/script.js" type="text/javascript"></script>

和某个地方的 UpdatePanel。该脚本写入一些内容,并在 js 文件中的匿名 JavaScript 函数中执行此操作。即,脚本中有类似这样的内容:

(function(){document.write('content');})();

每当通过异步回发更新 UpdatePanel 时,脚本所做的一切(或我页面上的任何 javascript)都会被撤消。 对于普通的 javascript,我只会使用:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myFunction)

来重做所有这些,但由于脚本源文件中的函数是匿名的并且在定义时调用,所以我很 SOL!有什么想法吗?

注意:外部js源来自另一个域,其内容不在我的控制范围内。

I have an external javascript on my page, e.g. something like:

<script src="http://foo.com/script.js" type="text/javascript"></script>

and an UpdatePanel somewhere. The script writes some content, and does this from within an anonymous javascript function in the js file. I.e., there is something like this in the script:

(function(){document.write('content');})();

Whenever the UpdatePanel is updated through asynchronous postback, everything the script did (or any javascript on my page, for that matter) is made undone.
For normal javascript, I would just use:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myFunction)

to redo all that, but since the function in the script source file is anonymous and called upon definition, I'm SOL! Any ideas?

Note: the external js source is from another domain and its content is out of my control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2024-08-20 02:55:01

尝试这个

private string _myScript = @"(function (){
                            var ys = document.createElement('script');
                            ys.type='text/javascript'; ys.async=true;
                            ys.src='http://foo.com/script.js';
                            var s = document.getElementsByTagName('script')[0];
                            s.parentNode.insertBefore(ys,s);
                            });";

然后在您的 Page_Load 中

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myScript", _myScript , true); 

Try this

private string _myScript = @"(function (){
                            var ys = document.createElement('script');
                            ys.type='text/javascript'; ys.async=true;
                            ys.src='http://foo.com/script.js';
                            var s = document.getElementsByTagName('script')[0];
                            s.parentNode.insertBefore(ys,s);
                            });";

Then in your Page_Load

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myScript", _myScript , true); 
你爱我像她 2024-08-20 02:55:01

好的,我想出了“解决方案”(“肮脏丑陋的黑客”,如果你愿意的话):我

不是直接加载 js 文件,而是通过读取文件的包装器加载它,将结果包装在自定义 javascript 中,将全局数组中的匿名函数,并在加载时和每次异步回发后调用该数组中的所有函数。

请不要在任何选美比赛中输入此解决方案。

Ok, the "solution" ("dirty ugly hack", if you prefer) I came up with:

Instead of loading the js file directly, I load it via a wrapper that reads the file, wraps the result in custom javascript that puts the anonymous function in a global array, and call all functions in said array upon load and after each asynchronous postback.

Please don't enter this solutions in any beauty pageants.

许你一世情深 2024-08-20 02:55:01

这里真正的问题是我没有正确使用 UpdatePanels。如果页面上所有 UpdatePanel 的 UpdateMode 设置为 Conditional,并且 ScriptManager 启用了部分更新,那么它确实不应该“[撤消]脚本所做的一切”。

The real problem here was that I wasn't using UpdatePanels correctly. If the UpdateMode of all the UpdatePanels on your page are set to Conditional, and your ScriptManager has partial updating enabled, it really shouldn't "[undo] everything the script did".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文