2013-05-26 153 views
1

我在我的網站中使用免費的天氣預報小部件。刷新JS腳本無需重新加載頁面

<script type="text/javascript" src="http://tools.tititudorancea.com/weather_forecast.js?place=zurich_2&amp;s=1&amp;days=5&amp;utf8=no&amp;columns=5&amp;temp=c"></script> 
<div style="font: 10px Arial, sans-serif; color: #000000" align="right"> 
<a href="http://www.tititudorancea.com/z/weather.htm">Weather forecast</a> provided by <a href="http://www.tititudorancea.com/">tititudorancea.com</a></div> 

我的網頁永遠不會自動重新加載,然後天氣永遠不會更新。 有沒有刷新JavaScript的方法,而無需刷新所有頁面?

在此先感謝

回答

0

這是我找到的解決方案。

var weatherURLSite = "http://tools.tititudorancea.com/weather_forecast.js?place=$placeID$&s=1&days=5&utf8=no&columns=5&temp=c";  
var specificWeather = weatherURLSite.replace("$placeID$", value); 
$.ajax({ 
      type: "POST", 
      async: false, 
      url: weatherURL, 
      data: { url: specificWeather } 
     }).done(
      function (content) { 
       var li = "<li id='" + value + "'>" + content + creditsDiv + "</li>"; 
       $("#weather").append(li);     
      }); 

而且我添加了一個定時器來完成刷新定期

var weatherTimer = self.setInterval(function() { GetReloadWeather('@Url.Action("GetWeather","Weather")'); }, 3600000); 
2

您只需在URL的末尾添加一個虛假的查詢VAR強制刷新。先將腳本標記添加到您的腳本標記中,然後再試試:

var weather = document.getElementById('weather'); 

weather.src += '&bogus='+ new Date(); // init 

setInterval(function() { 
    weather.src = weather.src.replace(/&.+$/, '&bogus='+ new Date()); 
}, 1000); 
+0

我想你的解決方案,但遺憾的是它沒有工作,天氣不會自我更新。任何想法? – LotuX

0

在我的項目我結束了jQuery的「replaceWith」功能

jQuery("script").eq(2).replaceWith('<script type="text/javascript" src="/state.js"></script>'); 
相關問題