如何将PHP模板添加到动态生成的Javascript代码中
我正在使用代码点火器和谷歌地图库。该库动态生成大量Javascript代码,包括每个新标记的InfoWindows内容,但我希望将其保存在单独的模板文件中,就像常规视图一样 我有以下Javascript代码(来自谷歌地图库): 我要做的是从模板文件加载如何将PHP模板添加到动态生成的Javascript代码中,php,javascript,codeigniter,Php,Javascript,Codeigniter,我正在使用代码点火器和谷歌地图库。该库动态生成大量Javascript代码,包括每个新标记的InfoWindows内容,但我希望将其保存在单独的模板文件中,就像常规视图一样 我有以下Javascript代码(来自谷歌地图库): 我要做的是从模板文件加载windowContent。我已经成功地为这个变量动态生成了一个表单,并使用了上面定义的lat和long变量,但是如何在CodeIgniter中实现这一点呢?我不能使用load->view,因为我不在控制器的上下文中。由于CI的安全限制,我不能使用
windowContent
。我已经成功地为这个变量动态生成了一个表单,并使用了上面定义的lat
和long
变量,但是如何在CodeIgniter中实现这一点呢?我不能使用load->view
,因为我不在控制器的上下文中。由于CI的安全限制,我不能使用include()
或readfile()
有什么提示吗?使用纯javascript,获取lat和long,在查询字符串中使用lat和long创建url,并使用xhr进行ajax调用
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var xhr;
var url = "http://myurl.to/script.php?lat="+lat+"&lng="+long;
if(typeof XMLHttpRequest !== 'undefined')
xhr = new XMLHttpRequest();
else {
//Get IE XHR object
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"];
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
}
}
xhr.onreadystatechange = function(){
//This function is called every so often with status updates
//It is complete when status is 200 and readystate is 4
if(xhr.status == 200 && xhr.readyState === 4) {
//Returned data from the script is in xhr.responseText
var windowContent = xhr.responseText;
//Create the info window
var newIW = new google.maps.InfoWindow( { content: windowContent } );
//Pass newIW to whatever other function to use it somewhere
}
};
xhr.open('GET', url, true);
xhr.send();
将contentWindow内容放入一个文件,并使用ajax检索该文件,然后在回调中为googlemaps infowindow编写初始化代码。您能详细说明一下吗?我不知道你所说的初始化代码是什么意思是初始化代码。由于ajax是一个异步过程,因此只有在ajax调用返回内容后,您才能执行代码。因此,在本次初始化中,将windowContent替换为ajax调用,传递lat和long-as参数以获取模板?添加了一个答案,显示ajax调用在代码中是什么样子的。我尝试了代码的简化jQuery版本,函数调用windowContent=load\u content(lat,long)。被调用的URL工作正常,我希望它返回“数据”,但不知何故它不工作。我甚至用alert(数据)进行了测试,它们都在那里,但没有返回。也许这与加载Ajax调用所需的时间有关。Ajax是异步的,调用它时不会返回数据,这就是为什么必须在回调函数中获取数据,
returndata回调函数中的code>不会像在windowContent=load\u content(lat,long)
的上下文中那样将其返回到windowContent变量,假设load\u content是一个包装函数,用于执行ajax调用本身。非常感谢您提供的代码和信息。在很多概念上都很有帮助。我一回到家,就会按照您的建议尝试在Ajax调用中设置infowindow内容。:)
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var xhr;
var url = "http://myurl.to/script.php?lat="+lat+"&lng="+long;
if(typeof XMLHttpRequest !== 'undefined')
xhr = new XMLHttpRequest();
else {
//Get IE XHR object
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"];
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
}
}
xhr.onreadystatechange = function(){
//This function is called every so often with status updates
//It is complete when status is 200 and readystate is 4
if(xhr.status == 200 && xhr.readyState === 4) {
//Returned data from the script is in xhr.responseText
var windowContent = xhr.responseText;
//Create the info window
var newIW = new google.maps.InfoWindow( { content: windowContent } );
//Pass newIW to whatever other function to use it somewhere
}
};
xhr.open('GET', url, true);
xhr.send();
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var url = "http://myurl.to/script.php";
jQuery.ajax({
"url":url,
"data":{ //Get and Post data variables get put here
"lat":lat,
"lng":long
},
"dataType":"html", //The type of document you are getting, assuming html
//Could be json xml etc
"success":function(data) { //This is the callback when ajax is done and successful
//Returned data from the script is in data
var windowContent = data;
//Create the info window
var newIW = new google.maps.InfoWindow( { content: windowContent } );
//Pass newIW to whatever other function to use it somewhere
}
});