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