我正在尝试将对象从函数中传递出去。这是我的代码:
<script type="text/javascript">
// finds the head element; creates a script with passed url; appends it to the head
function loadJSON(url) {
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript'; // isn't this 'application/json' really?
newScript.src = url;
headID.appendChild(newScript);
}
function parseJSON(o) {
if (content == null) {
document.write("<p>parseJSON(): JSON failed.</p>");
return false;
}
isDST = "<strong>not</strong> ";
if ( o.rawOffset == o.dstOffset ) {
isDST = "";
}
document.getElementById("geonames").innerHTML = "<p>The time now is " + o.time + " in " + o.countryName +".</p>";
}
var lat = "47.01"; var long = "10.2"; // test data for austria
var jsonUrl = " {{URL}} ";
loadJSON(jsonUrl);
</script>
我想要做的不是在 parseJSON 函数中使用对象 o 我想将对象传递出去,以便在 loadJSON() 调用之后我可以对对象进行操作。我发现在 parse 函数中调用 innerHTML 并不“整洁”。
我已经尝试了很多迭代,例如在脚本开始时声明“var jsonOutput”,然后制作“jsonOutput = o”,但这只是让 jsonOutput 未定义。如果我尝试返回 jsonOutput,那么我不知道它被返回到哪里,无论如何我都无法使用它。
我需要创建一个全局变量吗?我尝试了一个简单的“全局 jsonOutput”声明,但它似乎没有在函数内部进行修改。
卡住了,请帮忙。