| 1 |
/* |
| 2 |
|
| 3 |
javascript for ajax like request to update the weather location |
| 4 |
and corresponding data on the fly |
| 5 |
|
| 6 |
*/ |
| 7 |
|
| 8 |
/* load widget for the first time on this page */ |
| 9 |
function wpf_load() |
| 10 |
{ |
| 11 |
// get cookie |
| 12 |
if(document.cookie){ |
| 13 |
a = document.cookie; |
| 14 |
cookiename = a.substring(0,a.indexOf('=')); |
| 15 |
if(a.indexOf(';') != -1){ |
| 16 |
cookiewert = a.substring(a.indexOf('=')+1,a.indexOf(';')); |
| 17 |
} else { |
| 18 |
cookiewert = a.substr(a.indexOf('=')+1,a.length); |
| 19 |
} |
| 20 |
// set selected location |
| 21 |
if (cookiename == 'location') |
| 22 |
document.getElementById("wpf_selector").value = cookiewert; |
| 23 |
} |
| 24 |
// update widget |
| 25 |
wpf_update(); |
| 26 |
} |
| 27 |
/* get the data for the new location */ |
| 28 |
function wpf_update() |
| 29 |
{ |
| 30 |
var newloc = document.getElementById("wpf_selector").value; |
| 31 |
var siteuri = document.getElementById("wpf_selector_site").value; |
| 32 |
var language = document.getElementById("wpf_language").value; |
| 33 |
|
| 34 |
// set cookie with newloc |
| 35 |
var expire = new Date(); |
| 36 |
expire = new Date(expire.getTime() +1000*60*60*24*365); |
| 37 |
document.cookie = escape("location=" + newloc) + |
| 38 |
'; expires='+expire.toGMTString()+';'; |
| 39 |
|
| 40 |
jQuery.get(siteuri + "/wp-forecast-show.php", |
| 41 |
{ wpfcid: newloc, header: "0" , selector: "1" , language_override: language }, |
| 42 |
function(data){ |
| 43 |
/* |
| 44 |
var b = data.indexOf(">"); |
| 45 |
var e = data.lastIndexOf("<"); |
| 46 |
data = data.substring(b + 1, e - 1); |
| 47 |
*/ |
| 48 |
jQuery("div#wp-forecastA").html(data); |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
/* javascript to rebuild the onLoad event for triggering |
| 53 |
the first wpf_update call */ |
| 54 |
|
| 55 |
//create onDomReady Event |
| 56 |
window.onDomReady = initReady; |
| 57 |
|
| 58 |
// Initialize event depending on browser |
| 59 |
function initReady(fn) |
| 60 |
{ |
| 61 |
//W3C-compliant browser |
| 62 |
if(document.addEventListener) { |
| 63 |
document.addEventListener("DOMContentLoaded", fn, false); |
| 64 |
} |
| 65 |
//IE |
| 66 |
else { |
| 67 |
document.onreadystatechange = function(){readyState(fn)} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
//IE execute function |
| 72 |
function readyState(func) |
| 73 |
{ |
| 74 |
// DOM is ready |
| 75 |
if(document.readyState == "interactive" || document.readyState == "complete") |
| 76 |
{ |
| 77 |
func(); |
| 78 |
} |
| 79 |
} |