Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload external javascript after asynchronous postback via UpdatePanel

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.

like image 840
Protector one Avatar asked Jan 29 '26 03:01

Protector one


1 Answers

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); 
like image 188
Ani Avatar answered Jan 31 '26 15:01

Ani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!