Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Mini Project Template</title>
- </head>
- <body>
- City <input type="text" id="inpSearch"> <input type="button" id="btnSearch" value="Search" onclick="search();">
- <div id="result"/>
- <div id="temp"/>
- <script>
- function search() {
- var keyword = document.querySelector("#inpSearch").value;
- var baseUrl = "http://api.openweathermap.org/data/2.5/weather?q=";
- var url = baseUrl + keyword;
- var xmlhttp;
- if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
- xmlhttp=new XMLHttpRequest();
- } else {// code for IE6, IE5
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- xmlhttp.onreadystatechange=function() {
- if (xmlhttp.readyState==4 && xmlhttp.status==200) {
- // 4. Display data
- display(JSON.parse(xmlhttp.responseText));
- }
- }
- xmlhttp.open("GET", url, true);
- xmlhttp.send();
- }
- function display(data) {
- document.querySelector("#result").innerHTML = "The " + document.querySelector("#inpSearch").text + data.weather[0].description;
- document.querySelector("#temp").innerHTML = "The current temperature of " + document.querySelector("#inpSearch").value + " is " + Math.round(data.main.temp - 273.15) + "°C";
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement