Rebooting external javascript after asynchronous postback via UpdatePanel

I have external javascript on my page, for example. sort of:

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

and UpdatePanel somewhere. The script writes some content and does it from within an anonymous javascript function in the js file. For example, the script has something like this:

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

Whenever UpdatePanel is updated via asynchronous postback, everything that the script (or any javascript on my page has done, for that matter) is canceled. For regular javascript, I would just use:

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

repeat all this, but since the function in the source script file is anonymous and called by definition, I SOL! Any ideas?

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

+3
3

, "" ( " ", ) :

, js, , , javascript, .

, .

+2

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); 
+3

The real problem was that I used UpdatePanels incorrectly. If the UpdateMode parameter of all the UpdatePanels pages on your page is set to "Conditional" and your ScriptManager has a partial update, it should not "cancel" everything that the script did.

+1
source

Source: https://habr.com/ru/post/1724429/


All Articles