I have an external javascript on my page, e.g. something like:
<script src=".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.
I have an external javascript on my page, e.g. something like:
<script src="http://foo./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.
Share Improve this question edited Dec 3, 2009 at 4:33 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Dec 1, 2009 at 20:38 Protector oneProtector one 7,3595 gold badges67 silver badges88 bronze badges 3-
It doesn't matter that your function is anonymous. You can name it and call it globally just fine. The real problem is going to be
document.write
. Calling it after page load means the entire static page content is going to get wiped. – Crescent Fresh Commented Dec 1, 2009 at 21:08 - Actually, I'm not even sure the external javascript does a document.write. It might just create a new document node and append it to the page. The script's a big obfuscated mess, and there are more than one, actually. The problem is, I have no control over the content of these scripts. They have that anonymously called function, and I simply have to deal with it. – Protector one Commented Dec 2, 2009 at 12:42
- I have just verified it indeed does a document.write and just encountered the 'wipe' Crescent prophesized. Holy crap monkey balls. – Protector one Commented Dec 3, 2009 at 13:01
3 Answers
Reset to default 3Try this
private string _myScript = @"(function (){
var ys = document.createElement('script');
ys.type='text/javascript'; ys.async=true;
ys.src='http://foo./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);
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.
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".