Graham Carling
Graham Carling

Reputation: 563

How can I have C# code run after a jquery script that is running on page load?

I have a webpage that loads and runs jquery once the page is ready, like this:

$(document).ready(function () {
        navigator.geolocation.getCurrentPosition(showPosition, positionError);

        function showPosition(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            document.getElementById('div_latitude').innerHTML = latitude;
            document.getElementById('div_longitude').innerHTML = longitude;
        }

        function positionError(position) {
            alert("Error: " + position.code);
        }
    });

After I have assigned the div_latitude and div_longitude their values, I want to run code that will use this information automatically. How can I make sure this second piece of code will run after the jquery?

Upvotes: 1

Views: 513

Answers (3)

G . R
G . R

Reputation: 541

You will have to make an ajax call to communicate to the server side code so that you can pass the vales you needed after jQuery is loaded.

Code is as follows. $(document).ready(function () { navigator.geolocation.getCurrentPosition(showPosition, positionError);

    function showPosition(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        document.getElementById('div_latitude').innerHTML = latitude;
        document.getElementById('div_longitude').innerHTML = longitude;
    }

    function positionError(position) {
        alert("Error: " + position.code);
    }
     makeAjaxCall();

});
  function makeAjaxCall()
   {
     $.ajax({
          type: "POST",
          url: "PageName.aspx/MethodName",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
          // Ajax Call Successfull.
           }
       });
    }

Upvotes: 1

Rakesh Gupta
Rakesh Gupta

Reputation: 21

You might like to check status in jquery as "complete" and you can make an AJAX call to C# codes you want to execute.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190945

You will have to make an AJAX call up to the server.

Upvotes: 4

Related Questions