javascript - call java script function on page load in asp.net using c# -


i wants call java script function on page load in asp.net using c#, code is, asp.net code:

<%@ page language="c#" autoeventwireup="true" codebehind="lat_log_record.aspx.cs" inherits="lat_log.lat_log_record" %>  <!doctype html>  <html xmlns="http://www.w3.org/1999/xhtml">  <head runat="server">     <title></title> </head>  <body onload="getloc();">     <form id="form1" runat="server">         <script src="http://maps.google.com/maps/api/js?sensor=false">         </script>          <script type="text/javascript">         function getloc() {             if (navigator.geolocation) {                 navigator.geolocation.getcurrentposition(function(position) {                     var latitude = position.coords.latitude;                     var longitude = position.coords.longitude;                     document.getelementbyid("<%=hflat.clientid %>").value = latitude;                     document.getelementbyid("<%=hflon.clientid %>").value = longitude;                     var coords = new google.maps.latlng(latitude, longitude);                     var mapoptions = {                         zoom: 15,                         center: coords,                         maptypecontrol: true,                         navigationcontroloptions: {                             style: google.maps.navigationcontrolstyle.small                         },                         maptypeid: google.maps.maptypeid.roadmap                     };                     map = new google.maps.map(                         document.getelementbyid("mapcontainer"), mapoptions                     );                     var marker = new google.maps.marker({                         position: coords,                         map: map,                         title: "your current location!"                     });                  });             } else {                 alert("geolocation api not supported in browser.");             }         }         </script>         <style type="text/css">         #mapcontainer {             height: 500px;             width: 800px;             border: 10px solid #eaeaea;         }         </style>         <div id="mapcontainer" runat="server" visible="false">         </div>         <asp:hiddenfield id="hflat" runat="server" />         <asp:hiddenfield id="hflon" runat="server" />         <asp:button id="btnsave" runat="server" text="login" onclick="btnsave_click" />         <asp:gridview id="gvloc" runat="server">         </asp:gridview>         <asp:label id="lbllat" runat="server"></asp:label>         <asp:label id="lbllog" runat="server"></asp:label>      </form> </body>  </html> 

c# code:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.io; using system.net; using system.text; using system.data;  namespace lat_log {     public partial class lat_log_record : system.web.ui.page     {         class1 ob = new class1();         list<class1> lst = new list<class1>();         protected void page_load(object sender, eventargs e)         {              scriptmanager.registerstartupscript(this, this.gettype(), "javascript", "javascript:getloc();", true);             ob.lat = hflat.value;             ob.log = hflon.value;             lst.add(ob);             // lbllat.text = latitude;             // lbllog.text = longitude;             gvloc.datasource = lst;             gvloc.databind();         }           protected void btnsave_click(object sender, eventargs e)         {          }     }  } 

i wants run java script on page load shows blank grid... tried other option like... , winodows.onload in script not working, need thank you..!

use domcontentloaded event:

the domcontentloaded event fired when document has been loaded , parsed, without waiting stylesheets, images, , subframes finish loading (the load event can used detect fully-loaded page).

document.addeventlistener("domcontentloaded", function (event) {     console.log("dom loaded , parsed");     getloc();     // ^^^^^ }); 

documentation

https://stackoverflow.com/a/29773084/2025923

edit

you have nested function getloc() inside a(). reason getloc() not invoked. can remove function a() { , closing bracket }.

otherwise, use following code:

function a() {     function getloc() {         ...     }     getloc(); // call when `a` called } 

edit-2

remove return body onload

<body onload="getloc();"> 

demo: https://jsfiddle.net/tusharj/yfpf3ksr/


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -