Yahoo weather alert when it's raining - javascript
I'm trying to da when it's raining outside that I got "It's raining" on my page. But there is a problem. I allwys get "It's raining" evein when it's sunny. Here's my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body>
<script>
// Docs at http://simpleweatherjs.com
$(document).ready(function() {
$.simpleWeather({
location: 'Grad Zagreb, Croatia',
woeid: '',
unit: 'c',
success: function(weather) {
html = '<h2><i class="icon-'+weather.code+'"></i> '+weather.temp+'°'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li>';
html += '<li>'+weather.wind.direction+' '+weather.wind.speed+' '+weather.units.speed+'</li></ul>';
$("#weather").html(html);
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});
if(weather.code == 5 || weather.code == 6 || weather.code == 10 || weather.code == 35){
$("#oprez").html('It's raining!');}
});
(function(e){"use strict";e.extend({simpleWeather:function(t){t=e.extend({location:"",woeid:"2357536",unit:"f",success:function(e){},error:function(e){}},t);var n=new Date;var r="//query.yahooapis.com/v1/public/yql?format=json&rnd="+n.getFullYear()+n.getMonth()+n.getDay()+n.getHours()+"&diagnostics=true&callback=?&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=";if(t.location!==""){r+='select * from weather.forecast where woeid in (select woeid from geo.placefinder where text="'+t.location+'" and gflags="R") and u="'+t.unit+'"'}else if(t.woeid!==""){r+="select * from weather.forecast where woeid="+t.woeid+' and u="'+t.unit+'"'}else{t.error("Could not retrieve weather due to an invalid location.");return false}e.getJSON(r,function(n){if(n!==null&&n.query.results!==null&&n.query.results.channel.description!=="Yahoo! Weather Error"){e.each(n.query.results,function(e,n){if(n.constructor.toString().indexOf("Array")!==-1){n=n[0]}var r=["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"];var i=r[Math.round(n.wind.direction/22.5)];if(n.item.condition.temp<80&&n.atmosphere.humidity<40){var s=-42.379+2.04901523*n.item.condition.temp+10.14333127*n.atmosphere.humidity-.22475541*n.item.condition.temp*n.atmosphere.humidity-6.83783*Math.pow(10,-3)*Math.pow(n.item.condition.temp,2)-5.481717*Math.pow(10,-2)*Math.pow(n.atmosphere.humidity,2)+1.22874*Math.pow(10,-3)*Math.pow(n.item.condition.temp,2)*n.atmosphere.humidity+8.5282*Math.pow(10,-4)*n.item.condition.temp*Math.pow(n.atmosphere.humidity,2)-1.99*Math.pow(10,-6)*Math.pow(n.item.condition.temp,2)*Math.pow(n.atmosphere.humidity,2)}else{var s=n.item.condition.temp}if(t.unit==="f"){var o="c";var u=Math.round(5/9*(n.item.condition.temp-32));var a=Math.round(5/9*(n.item.forecast[0].high-32));var f=Math.round(5/9*(n.item.forecast[0].low-32));var l=Math.round(5/9*(n.item.forecast[1].high-32));var c=Math.round(5/9*(n.item.forecast[1].low-32));var h=Math.round(5/9*(n.item.forecast[1].high-32));var p=Math.round(5/9*(n.item.forecast[1].low-32));var d=Math.round(5/9*(n.item.forecast[2].high-32));var v=Math.round(5/9*(n.item.forecast[2].low-32));var m=Math.round(5/9*(n.item.forecast[3].high-32));var g=Math.round(5/9*(n.item.forecast[3].low-32));var y=Math.round(5/9*(n.item.forecast[4].high-32));var b=Math.round(5/9*(n.item.forecast[4].low-32))}else{var o="f";var u=Math.round(9/5*n.item.condition.temp+32);var a=Math.round(9/5*n.item.forecast[0].high+32);var f=Math.round(9/5*n.item.forecast[0].low+32);var l=Math.round(5/9*(n.item.forecast[1].high+32));var c=Math.round(5/9*(n.item.forecast[1].low+32));var h=Math.round(5/9*(n.item.forecast[1].high+32));var p=Math.round(5/9*(n.item.forecast[1].low+32));var d=Math.round(5/9*(n.item.forecast[2].high+32));var v=Math.round(5/9*(n.item.forecast[2].low+32));var m=Math.round(5/9*(n.item.forecast[3].high+32));var g=Math.round(5/9*(n.item.forecast[3].low+32));var y=Math.round(5/9*(n.item.forecast[4].high+32));var b=Math.round(5/9*(n.item.forecast[4].low+32))}var w={title:n.item.title,temp:n.item.condition.temp,tempAlt:u,code:n.item.condition.code,todayCode:n.item.forecast[0].code,units:{temp:n.units.temperature,distance:n.units.distance,pressure:n.units.pressure,speed:n.units.speed,tempAlt:o},currently:n.item.condition.text,high:n.item.forecast[0].high,highAlt:a,low:n.item.forecast[0].low,lowAlt:f,forecast:n.item.forecast[0].text,wind:{chill:n.wind.chill,direction:i,speed:n.wind.speed},humidity:n.atmosphere.humidity,heatindex:s,pressure:n.atmosphere.pressure,rising:n.atmosphere.rising,visibility:n.atmosphere.visibility,sunrise:n.astronomy.sunrise,sunset:n.astronomy.sunset,description:n.item.description,thumbnail:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.condition.code+"ds.png",image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.condition.code+"d.png",tomorrow:{high:n.item.forecast[1].high,highAlt:l,low:n.item.forecast[1].low,lowAlt:c,forecast:n.item.forecast[1].text,code:n.item.forecast[1].code,date:n.item.forecast[1].date,day:n.item.forecast[1].day,image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.forecast[1].code+"d.png"},forecast
s:{one:{high:n.item.forecast[1].high,highAlt:h,low:n.item.forecast[1].low,lowAlt:p,forecast:n.item.forecast[1].text,code:n.item.forecast[1].code,date:n.item.forecast[1].date,day:n.item.forecast[1].day,image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.forecast[1].code+"d.png"},two:{high:n.item.forecast[2].high,highAlt:d,low:n.item.forecast[2].low,lowAlt:v,forecast:n.item.forecast[2].text,code:n.item.forecast[2].code,date:n.item.forecast[2].date,day:n.item.forecast[2].day,image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.forecast[2].code+"d.png"},three:{high:n.item.forecast[3].high,highAlt:m,low:n.item.forecast[3].low,lowAlt:g,forecast:n.item.forecast[3].text,code:n.item.forecast[3].code,date:n.item.forecast[3].date,day:n.item.forecast[3].day,image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.forecast[3].code+"d.png"},four:{high:n.item.forecast[4].high,highAlt:y,low:n.item.forecast[4].low,lowAlt:b,forecast:n.item.forecast[4].text,code:n.item.forecast[4].code,date:n.item.forecast[4].date,day:n.item.forecast[4].day,image:"//l.yimg.com/a/i/us/nws/weather/gr/"+n.item.forecast[4].code+"d.png"}},city:n.location.city,country:n.location.country,region:n.location.region,updated:n.item.pubDate,link:n.item.link};t.success(w)})}else{if(n.query.results===null){t.error("An invalid WOEID or location was provided.")}else{t.error("There was an error retrieving the latest weather information. Please try again.")}}});return this}})})(jQuery)
</script>
<div id="weather"></div>
<div id="oprez"></div>
</body>
</html>
Need to add more text,texte,text,text,sorry for this
Related
Youtube api -how to return nextpage token from fuction?
Using youtube api to get playlist items. There are more than 50 in the list so will need to build a loop to get them. When a button is clicked I'm calling a function that will return 50 at a time then will build a loop to get all items. Having problems returning the nextpagetoken from the function. I couldn't figure out a way to return the data so got to a point where I've just plugged the nextpagetoken into the html of a div inside the function but, once the function finishes when I try to get the html value it returns blank. The console.log('Nextpagetoken: ' + data.nextPageToken); command shows the correct value. The console.log('After call: ' + next); command shows a blank value. Also, the 'After call' command appears in the console log before the 'Nextpagetoken' log. Any help greatly received. Here's the javascript and html: $(document).ready(function() { var loop=0; var data; var next=''; $('#get').click(function(){ getVids(); next=$('#pagetoken').text(); console.log('After call: ' + next); }); function getVids(page){ $('#results').html(""); data = { part: 'snippet', maxResults: 50, playlistId: 'playlist', key: 'myKey', pageToken: page }; $.get( "https://www.googleapis.com/youtube/v3/playlistItems", data, function(data){ var output; console.log('Page: ' + data.nextPageToken); $.each(data.items, function(i, item) { console.log(item); videoTitle = item.snippet.title; rvideoID = item.snippet.resourceId.videoId; vidThumburl = item.snippet.thumbnails.high.url; output = '<li style="list-style:none;">' + rvideoID + ' <a class="videos2 video" href="https://www.youtube.com/watch?v=' + rvideoID + '">' + videoTitle + '<a/></li>'; //Append to results list $('#results').append(output); }); console.log('Nextpagetoken: ' + data.nextPageToken); $('#pagetoken').html(data.nextPageToken); }); } }); Here's the html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script src="yt.js"></script> </head> <body> <div id="container"> <button id='get' type="button" class="btn btn-primary">Go</button> <ul id="results"></ul> <div id='pagetoken'></div> </div> </div> </body> </html>
Next is blank because getVids is async and the html isn't yet appended when you call console.log
why javascript doesnt run
I am learning JavaScript. I've written this code, but it does not seem to run. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <script language="javascript" type="text/javascript"> function nav(){ try{ var str="<table border=1>"; for (var n in navigator){ str+="<tr><td>" + n + "</td></tr>"; } str+="</table> return("jetzt wird das Ding aufgelistet: <br/>" + str); }catch(e){return(e);} } function writeit(){ document.write(nav()); } </script> <title>for learning purpos</title> </head> <body> <form> <script> document.write(nav()); </script> <p>press the button to get the properties in navigator</p> <input type="button" id="btnNavigator" value="get" onclick="writeit();"/> </form> </body> </html>
You haven't closed one string. str+="</table>";
Best practice is to look into the console from developer tab, inspect page. Also on that tab on the bottom, you can try javascript code or even jQuery if you have the library added. Your code it's wrong at line 13 you have Invalid or unexpected token because you didn't close the string and at line 30 nav it's not defined. You can use this code: function navBuild(n) { var tblS = "<table>"; var tblE = "</table>"; var strTrTd = ""; for (i=1; i<=n; i++) { strTrTd = strTrTd + "<tr><td>" + i + "</td></tr>"; } var strAll = tblS + strTrTd + tblE; console.log(strAll); document.getElementById("contentBox").innerHTML = strAll; } And in your HTML you could use: <input type="button" id="btnNavigator" value="get" onclick="navBuild(5);"/> <div id="contentBox"></div>
Code works in chrome and firefox but fails in IE9
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script language="javascript" type="text/javascript"> function abc() { ansArray = ['a']; document.write('<input type = "button" value = "a">'); document.write('<input type = "button" value = "b">'); var myButton = document.getElementsByTagName("input"); myButton[0].onclick = function() { if(ansArray[0] == 'a') myButton[0].style.backgroundColor = "green"; else myButton[0].style.backgroundColor = "red"; } myButton[1].onclick = function() { if(ansArray[0] == 'b') myButton[1].style.backgroundColor = "green"; else myButton[1].style.backgroundColor = "red"; } } </script> </head> <body onload="abc()"> </body> </html> This code segment is to change the colour of the two buttons on click event,works fine in chrome and firefox but the onclick functions does not work in IE9. Please help... Thanks in advance
Try calling the function like (function abc(){ // code here })(); Also use ; after each function expression, i.e. myButton[0].onclick = function() {...};. Working here.
converting html tags and the content to string
my question is I need to create a string from the result of event.content. Event.content returns me an entry including html tags. I can use it like container.innerHTML = event.content. I need the event.content as a string. I tried to do something like this: var a = '' + event.content; But it doesn't work. Here the result of event.content: <img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 ° F. For more details? I can't convert this into string in javascript. Is it possible? I also tried String(event.content). Ok I put my whole code. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1"> <title>Hava Durumu</title> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("feeds", "1"); function havabas(){ var feed = new google.feeds.Feed("http://rss.weather.com/weather/rss/local/TUXX0014? cm_ven=LWO&cm_cat=rss&par=LWO_rss"); feed.load(function(result) { if (!result.error) { var entry = result.feed.entries[0]; var container = document.getElementById("weather"); var c = entry.content; var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+\s*°)/; var results = regex.exec(entry.content); var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2]; container.innerHTML = html; } }); } function initialize() { havabas(); setInterval(function(){havabas()},2000); } google.setOnLoadCallback(initialize); </script> </head> <body> <div id="weather"></div> </body> </html>
Your regex is invalid. It does not match the string from entry.content. The reason for this is °. Change your code to this: var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+)/; var results = regex.exec(entry.content); var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2] + ' °'; DEMO
Probably you forgot to escape either the sigle quotes or double quotes with a backlash? This is a valid string - var string="<img src=\"http://image.weather.com/web/common/wxicons/31/30.gif?12122006\" alt=\"\" />Partly Cloudy, and 84 ° F. For more details?";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <script> function get1() { var i='<img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 ° F. For more details?'; var j='\'' +i+ '\''; alert(j); } </script> <BODY onload='get1();'> </BODY> </HTML> Please try this
I lose focus from the main page
I am opening a window that is not in my domain and I can not do to maintain the focus on my window. This is the code. The work is only for IE. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { var win = window; var myOptions = "width=300,height=250,top=300px,left=300px,toolbar=0,location=0,status=0,scrollbars=0,menubar=0,directories=0,resizable=0"; var myWinReplace = true; var myWinName = "xwin"; var myUrl = "http://www.google.com"; var myOpen = window.open(myUrl, myWinName , myOptions, myWinReplace); window.focus(); //option 1 $(myOpen).load(function(){ window.focus(); //option 2 win.focus(); //option 3 setTimeout(function() { window.focus(); }, 500); //option 4 setTimeout(function() { win.focus(); }, 1500); //option 5 }); }); </script> </head> <body> </body> </html>
I think you can do this: myOpen.opener.focus(); Here is a link for more information