繁体   English   中英

刷新JS脚本而无需重新加载页面

[英]refresh JS Script without reloading the page

我在网站上使用了免费的天气预报小部件。

<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而不刷新整个页面的方法?

提前致谢

您可以在网址末尾添加一个伪造的查询var来强制刷新。 首先向您的脚本标签添加ID,然后尝试以下操作:

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

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

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

这是我找到的解决方案。

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);

在我的项目中,我最终使用了jQuery'replaceWith'函数

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM