Изменить событие клика на загрузку страницы - PullRequest
0 голосов
/ 26 февраля 2020

У меня есть демо-код для API погоды. Мне нужно изменить его на событие щелчка, чтобы загрузить при открытии страницы. Как я go об этом. Я довольно новичок в jquery. Любая помощь будет приветствоваться

function bclicked() 
{ 
    //This function was for testing my button and containers
    console.log("CLICKED!");
    var node = document.createElement("P");
    var textnode = document.createTextNode("Placeholder for real weather!");
    node.appendChild(textnode);
    document.getElementById("weather").innerHTML = "";
    document.getElementById("weather").appendChild(node);
}

function getweather() 
{ 
    //Does a post request for the ZIP entered to the API
    console.log("REQUESTING WEATHER");
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () 
    {
        if (this.readyState == 4 && this.status == 200) {
            var weather = JSON.parse(this.responseText);
            console.log(weather);
            maketable(weather); //Generates content
        }
        else
        { 
            console.log("this.readyState=",this.readyState);
            console.log("this.status=",this.status);
        }
    };

    var dataprefix = "https://api.openweathermap.org/data/2.5/weather?zip=33431,us"
    var datasuffix = "&units=imperial&appid=925e764bc4519818e577ebdbae087ef8"
    var data = dataprefix + document.getElementById.value + datasuffix;
    console.log("requesting:",data);
    xmlhttp.open("POST", data, true)
    xmlhttp.send();
}

1 Ответ

0 голосов
/ 26 февраля 2020

Удалите функцию bclicked() и выполните вызов getweather(), как показано ниже. Это вызовет функцию getweather() при загрузке страницы.

getweather();

function getweather() 
{ 
    // function body
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...