Javascript 如何在加载网页时运行脚本

Javascript 如何在加载网页时运行脚本,javascript,Javascript,如何在加载网页时运行此脚本,而无需按下按钮 $(document).on('ready', function() { $('#findme').on('click', function() { $('#message').html('Loading...'); $.ajax( { url: "https://api.mercadolibre.com/geolocation/whereami", success:

如何在加载网页时运行此脚本,而无需按下按钮

    $(document).on('ready', function() {

    $('#findme').on('click', function() {    
    $('#message').html('Loading...');    
      $.ajax( {
        url: "https://api.mercadolibre.com/geolocation/whereami",
        success: function(data) {
          var city = data.city_name;
          var country = data.country_name;
          $('#message').text("You are in " + city + ", " + country + '!'); 
          }

      });
  });

});

你们可以试试这个,它是你们的代码,只有我删除了点击功能的按钮,所以它将在文档准备功能上执行

$(document).on('ready', function() {

$('#message').html('Loading...');    
  $.ajax( {
    url: "https://api.mercadolibre.com/geolocation/whereami",
    success: function(data) {
      var city = data.city_name;
      var country = data.country_name;
      $('#message').text("You are in " + city + ", " + country + '!'); 
      }

  });
});

将代码放入函数中,并在所需事件中调用它

$document.on'ready',函数{…}的使用在jQuery的第一个实例中就被弃用了,这可能是v1.8。最好是:

$(document).ready(function() { ... })


你试过扳机吗?只需使用window.onload=function{functionnameoff函数,该函数应运行ngeloadparam1,param2,…;};不,我尝试了window onload,而$document.ready函数{functionNameOfFunctionWhichShouldRunOnPageLoad;}的输出是什么?我正在尝试运行这个
$(document).ready(function() { ... })
$(window).on('load', function() { ... })