重写页面javascript到js文件

时间:2017-11-17 13:35:48

标签: javascript function

我使用一段Javascript在仪表板上显示当前天气。该函数以“inpage”格式写入,因此需要将它放在我的index.php中。我想将该函数移动到我的scripts.js,但是如何将此代码转换为正常工作?我对Javascript函数很新...有什么想法吗?

           reallySimpleWeather.weather({
               wunderkey: '', // leave blank for Yahoo
               location: 'Holten, NL', //your location 
               woeid: '', // "Where on Earth ID"
               unit: 'c', // 'c' also works

               success: function(weather) {
                 html = '<div id="weergraden">'+weather.temp+'&#176;'+' graden</div>';
                 html += '<div id="weerstatus">'+weather.currently.replace("Mostly ", "Veelal ").replace("Showers" , "Regenbuien").replace("Sunny", "Zonnig").replace("Partly ", "Lichte ").replace("Cloudy", "Bewolking").replace("Rain", "Regen").replace("Scattered " , "Verspreide ").replace("Clear", "Helder")+'</div>';
              html += '<img src="'+weather.image+'"></img>';
              //html += "<img src='"+serverurl+"weer/"+weather.currently.replace(" ", "_")+".png'></img>";
              document.getElementById('weer').innerHTML = html; 
              setInterval(weather, 6000);
               },
               error: function(error) {
              document.getElementById('weer').innerHTML = '<p>'+error+'</p>';
               }
           });

1 个答案:

答案 0 :(得分:0)

您可以将此代码复制到单独的文件中,例如app.js, 然后将其连接到您的php页面。

<?php echo"<script language='javascript' src='reallySimpleWeatherCDN'></script>";?>
<?php echo"<script language='javascript' src=/javascript/app.js></script>";?>

请考虑在reallySimpleWeather库之后连接app.js