【发布时间】:2012-12-08 08:05:38
【问题描述】:
每个人。我正在做一个只使用 JavaScript(客户端,而不是服务器端)的项目,我想使用我在另一个线程中找到的来自 NOAA 的脚本(我不能将多个链接列为新用户,不过)我发现了这个:
http://www.srrb.noaa.gov/highlights/sunrise/sunrise.html
我想要做的是,当用户输入纬度和经度并点击“提交”时,它将获取今天的日出/日落数据并将它们存储在两个变量中(例如日出和日落)。
然而,我真的希望他们可以将所有脚本(除了嵌入组合框中的脚本)放在一个单独的 JavaScript 文件中,这样我就可以将来自 NOAA 网站的 JavaScript 代码源提供到我的项目中一直在努力。
<div id="locationSetupDiv">
<fieldset>
<legend>Location</legend>
<p>
<a href="javascript:var load = window.open('http://www.esrl.noaa.gov/gmd/grad/solcalc/')">
Get your longitude and latitude from here.</a>
</p>
<table class="inputData">
<tr>
<th style="width: 75px;">
<label id="labelLongitude" for="inputModuleIDNumber">
Longitude:</label></th>
<td><input id="inputLongitude" /></td>
</tr>
<tr>
<th><label id="labelLatitude" for="inputModuleName">
Latitude:</label></th>
<td><input id="inputLatitude" /></td>
</tr>
</table>
<input type="button" value="Submit Changes"
style="float: right; margin-top: 1em;" />
</fieldset>
</div>
上面的代码是 HTML 格式的。现在,这是我的 JavaScript 代码:
function PopulateFromXML()
{
listModules = document.getElementById(
"listModules").getElementsByTagName("tbody");
// Gather the text fields for location data.
inputLongitude = document.getElementById("inputLongitude")
inputLatitude = document.getElementById("inputLatitude")
// Firefox's DOM Parser treats whitespaces as text nodes, including
// line breaks.
removeWhitespace(xmlDoc.documentElement);
// Send the document's root element to the XML Object variable.
xmlObj = xmlDoc.documentElement;
// Perform the checks and populate the tables
// and any other elements that are needed.
if (xmlObj.tagName == "HomeAutomationInterface")
{
// Check to be sure the first node is the "Setup" node. It contains the
// location node.
if ((xmlObj.childNodes[0].tagName == "Setup") &&
(xmlObj.childNodes[0].childNodes[0].tagName == "Location"))
{
// Copy the data from one of the attributes to the respective text boxes.
inputLongitude.value = xmlObj.childNodes[0]
.childNodes[0].getAttribute("longitude");
inputLatitude.value = xmlObj.childNodes[0]
.childNodes[0].getAttribute("latitude");
}
// The second node within the root element is Modules node.
// This will be implemented.
if (xmlObj.childNodes[1].tagName == "Modules")
{
ListModulesInTabularData();
}
// The third node within the root element is Scenes node.
// You need modules in order for scenes to work.
// This will be implemented.
if (xmlObj.childNodes[2].tagName == "Scenes")
{
ListScenesInTabularData();
}
// The last element is the Timers node.
// This will either activate the scene or turn on, off,
// dim, brighten, or set the light level of the module if
// it is the light module or turn on or off the appliance
// module. This will be implemented.
if (xmlObj.childNodes[3].tagName == "Timers")
{
ListTimersInTabularData();
}
}
}
如您所见,字段集中的两个文本框是在从 XML 文件填充时写入的。虽然我正在做一个项目作为概念验证,但我找不到将所有数据保存到 XML 文件的方法,所以我的老师告诉我不要担心。但是由于我编写了一个代码来以表格格式动态填充所有 XML 数据,所以我一直在 Internet 上寻找特定的 JavaScript 代码,或者可能是我可以从我的项目链接到的 JavaScript 文件,但是使用我从 NOAA 为我的个人项目借用的 JavaScript 代码,有人知道如何根据纬度、经度和今天的日期获取日出/日落时间吗?
更新
我好像在网上找到了一些东西(我在 Google 上搜索过:日出日落网络服务):
http://www.earthtools.org/webservices.htm
我使用关键字“sunrise Sunset JavaScript”(不带引号)进行了搜索,我花了一些时间进行搜索,但随后将“JavaScript”替换为“Web 服务”,我能够找到我正在寻找的网站是的,但是当涉及到 IE8/9 中的“同源策略”时,我真的不认为这将与 XMLHttpRequest 一起工作,这应该是为了防止跨站点脚本。
【问题讨论】:
-
基本相同的问题,javascript 已为您分解。它在接受的答案中链接:stackoverflow.com/questions/2056555/…
标签: javascript latitude-longitude