我正在使用谷歌地图API来建立我自己的世界。我遇到的问题是标记/位置的数据编号导致页面无响应。
所以我相信;可能是错的;解决方案是只调用边界中的标记/位置(即在窗口上可见)和缩放级别。
我想如果我得到窗口和缩放级别的边界并将其传递给将构建js标记函数然后调用该函数的php脚本。
但是,我尝试了一个基本的脚本,看起来它返回功能,但它不会激活脚本。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("script").innerHTML = this.responseText;
eval(this.responseText);
}
};
xhttp.open("GET", "fjack.php", true);
xhttp.send();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div onclick="loadDoc();"> click </div>
<div id="script"></div>
<div id="demo"></div>
</body>
</html>
这是php脚本:
<?php
echo "<script>";
echo "function me(){
document.getElementById(\"demo\").innerHTML=\"ITS WORKING\";
}
me();";
echo "</script>";
?>
我尝试过eval(),但没有做任何事情。
你几乎就在那里,只需从没有<script>
标签的服务器返回.js代码并使用eval评估字符串,所以你必须改变这个:
if (this.readyState == 4 && this.status == 200) {
document.getElementById("script").innerHTML = this.responseText;
eval(this.responseText);
}
有了这个:
if (this.readyState == 4 && this.status == 200) {
eval(this.responseText);
return;
}
和服务器(PHP脚本)响应如下所示:
<?php
echo "function me(){
console.log('hello world!');
}
me();";
它工作,但使用eval()不是最好的选择;我建议为此使用一个函数对象(新的函数('动态内容 - 这里的服务器响应')),或者甚至更好,一个js模块加载器,它检查模块的依赖关系并以正确的顺序动态加载所需的js文件,标题部分,使用<script>
标记,不需要XHR请求,因为在您之前的回答中已经提到过。看看http://requirejs.org