0

I have js code which gets which gets users lat long but the problem is when I call the js function in my asp.net control button it does not work and no alert is shown and the function getLocation does not work

var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "Geolocation is not supported by this browser."; } }

function showPosition(position) {
   // var latlondata =  position.coords.latitude + "," +position.coords.longitude;
    var latlon =  position.coords.latitude;
    alert(latlon)

    document.getElementById('<%=abc123.ClientID%>').innerText = latlon;


}

function showError(error) {
    if (error.code == 1) {
        x.innerHTML = "User denied the request for Geolocation."
    }
    else if (err.code == 2) {
        x.innerHTML = "Location information is unavailable."
    }
    else if (err.code == 3) {
        x.innerHTML = "The request to get user location timed out."
    }
    else {
        x.innerHTML = "An unknown error occurred."
    }
}

and here is my control code

<asp:Label ID="abc123" runat="server" Text="Label"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getLocation()" OnClick="Button1_Click " />
3
  • Did you make sure the JS works as expected? Commented Aug 25, 2014 at 9:05
  • yes js is working if i do a simple html button call like this <button onclick="getLocation()">Get your Location</button> but do not work for asp.net control Commented Aug 25, 2014 at 9:06
  • please add your "getLocation()" code function in your question. Commented Aug 25, 2014 at 12:50

2 Answers 2

1

The OnClick event will reload the page and therefore the JS called from the OnClientClick will never be triggered. Remove the OnClick event.

Edit:

If you need to trigger the serverside event let your JS trigger it after the alert.

Sign up to request clarification or add additional context in comments.

1 Comment

I have removed the OnClick but its not working , also I need OnClick too for c# code
0

Just add the following code to your button:

OnClientClick="getLocation(); return false;"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.