{"id":23,"date":"2008-03-07T20:37:16","date_gmt":"2008-03-08T04:37:16","guid":{"rendered":"http:\/\/shanelabs.com\/blog2\/?p=23"},"modified":"2011-12-15T21:45:08","modified_gmt":"2011-12-16T05:45:08","slug":"substitute-for-onload-with-webparts","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2008\/03\/07\/substitute-for-onload-with-webparts\/","title":{"rendered":"Substitute for onLoad with WebParts"},"content":{"rendered":"<p>I\u2019ve got a project where I\u2019m using google maps within an ASP.NET web part in an update panel. The google maps code requires you to run some javascript as soon as the page loads. Usually people put this in &lt;body onload=x&gt; or use javascript: window.onload=x, but when I did those either the method wouldn\u2019t run, or web parts would get messed up. I\u2019m guessing because there\u2019s some web parts javascript that needs to run at page load, and since you can only assign one method, they were interfering with eachother.<\/p>\n<p>I tried just running the google maps initialization script after I write all the needed elements to the page, but then I was randomly getting the dreaded\u00a0IE error \u201cInternet Explorer cannot open the internet site. Operation Aborted.\u201d\u00a0The common fixes to this error suggest calling your initialization script with an onload trigger (not possible for me as described above) or moving your script to\u00a0the bottom of the page, outside of the body tag.<\/p>\n<p>Sounds like an easy fix, but since I was using web parts within update panels\u00a0I had to use ScriptManager.RegisterStartupScript in order to inject the code, which didn\u2019t allow me to place exactly where the script was being placed. After long hours of trying many many workarounds, I finally found about the PageRequestManager pageLoaded event. It\u2019s a javascript class that is available when you use asp.net\u2019s ajax, and it can be used to register functions that should be triggered once the page is loaded. Here\u2019s the javascript you need:<\/p>\n<pre>var prm = Sys.WebForms.PageRequestManager.getInstance();\r\nprm.add_pageLoaded(myFunctionToRunOnLoad);<\/pre>\n<p>It took me forever to find this, I wish it was documented more. Hopefully it solves your problems!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve got a project where I\u2019m using google maps within an ASP.NET web part in an update panel. The google maps code requires you to run some javascript as soon as the page loads. Usually people put this in &lt;body onload=x&gt; or use javascript: window.onload=x, but when I did those either the method wouldn\u2019t run, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":5,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}