jquery - How to stop run javascript function when asp.net webform loaded? -


i'm write code show position on map:

<script src="http://maps.googleapis.com/maps/api/js"></script>    <script>        function initialize(x, y) {            alert(x);            alert(y);            var mapprop = {                center: new google.maps.latlng(x, y),                zoom: 5,                maptypeid: google.maps.maptypeid.roadmap            };            var map = new google.maps.map(document.getelementbyid("googlemap"), mapprop);        }        google.maps.event.adddomlistener(window, 'load', initialize); </script> 


code run initialize function when page load or postback,but write code asp button:

page.clientscript.registerstartupscript(this.gettype(), "beh", "initialize(38.076306,46.279637);", true);  


, want when button fire,send value function , java script function change map position,it's work,but when button fire,function run ,and run again when web form loaded!,and in first time show correct position,but when page load event occurred,send null value function , show empty position, think when run java script function in asp button fire,and not run java script function when page load occurred?

the problem code execution order of code blocks. execution time of code added via clientscriptmanager.registerstartupscript according msdn:

the script block added registerstartupscript method executes when page finishes loading before page's onload event raised.

so, solve problem adding boolean variable in script tag (which get's executed while page loading), set false , change true in button click handler (which executed once page has finished loading). in window.onload event listener, check if variable false. if is, can initialize map.

so, javascript change this:

<script>     var isbuttonclick = false;     function initialize(x, y) {         var mapprop = {             center: new google.maps.latlng(x, y),             zoom: 5,             maptypeid: google.maps.maptypeid.roadmap         };         var map = new google.maps.map(document.getelementbyid("googlemap"), mapprop);     }     google.maps.event.adddomlistener(window, 'load', function () { if(!isbuttonclick)initialize(0, 0) }); </script> 

and button click handler this:

void buttonclick(object sender, eventargs e) {     page.clientscript.registerstartupscript(this.gettype(), "beh", "isbuttonclick=true;initialize(38.076306,46.279637);", true); } 

i tested locally , worked fine


Comments