Javascript与IE和FF(调用函数)不兼容
我用javascript编写了一些代码,我需要它在大多数浏览器上工作,但它只在googleChrome中工作,IE和FF不调用此函数:Javascript与IE和FF(调用函数)不兼容,javascript,internet-explorer,firefox,incompatibility,Javascript,Internet Explorer,Firefox,Incompatibility,我用javascript编写了一些代码,我需要它在大多数浏览器上工作,但它只在googleChrome中工作,IE和FF不调用此函数: <script type="text/javascript"> showLocation(<?php print "\"$citta2\""?>,<?php print "\"$row[nome] $row[cognome]\""?>,<?php print "\"$row[id]\""?>); return
<script type="text/javascript">
showLocation(<?php print "\"$citta2\""?>,<?php print "\"$row[nome] $row[cognome]\""?>,<?php print "\"$row[id]\""?>); return false;
</script>
它使用谷歌地图api获取城市的纬度和经度不是答案,只是需要超过评论中允许的600个字符 首先,让您的脚本标记更易于阅读,并添加一些调试:
<script type="text/javascript">
try {
if (window.showLocation)
{
showLocation(<?php print "'{$citta2}','{$row[nome]} {$row[cognome]}','{$row[id]}'"; ?>);
}
else
{
// if you see this message:
// then you have a syntax error before or within the creation of showLocation
alert("showLocation has not been created!");
}
}
catch (e) {
alert("Error from caller: " + e);
}
</script>
function showLocation(address2,nomi0,id0) {
try {
alert("1");
var nomi1 = nomi0;
var id1 = id0;
geocoder.getLocations(address1, function (response)
{
try
{
if (!response || response.Status.code != 200)
{
alert("Errore con il primo indirizzo");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(address2, function (response)
{
try
{
if (!response || response.Status.code != 200)
{
alert("Errore con il secondo indirizzo");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
calculateDistance(nomi1,id1);
}
}
catch (e)
{
alert("Error in callback#2: "+ e);
}
});
}
}
catch (e)
{
alert("Error in callback#1: " + e);
}
});
}
catch (e)
{
alert("Error in functon: " + e);
}
}
_添加了更多调试警报\uushowus showLocation代码,可能有语法错误…如果某些JavaScript不起作用,请向我们展示JavaScript,而不是输出JavaScript的PHP…并向我们展示一个完整的测试用例,以演示问题。如果您知道它不会调用它,因为它要做的第一件事是生成一个allert,那么请包含一个仅用于此操作的showLocation版本。A也会很有帮助。添加了ShowLocation的代码你能发布你在IE/FF中得到的错误消息吗?在函数调用中添加try and catch会使它在chrome中不起作用,同时函数不会被调用,也不会发布错误,从调用中取出try-and-catch只会使它在chrome中正常工作,并且该函数不会对错误发出警报。我还尝试进行函数测试{alerttest;},并在我的页面中间使用test调用它;同样,只适用于chromeIm,认为在创建showLocation之前会抛出异常,这就是为什么不调用它。新代码在chrome中工作时没有错误,在firefox中不会生成任何内容,没有错误,没有结果,没有警报,没有任何内容=[这将非常有用:
function showLocation(address2,nomi0,id0) {
try {
alert("1");
var nomi1 = nomi0;
var id1 = id0;
geocoder.getLocations(address1, function (response)
{
try
{
if (!response || response.Status.code != 200)
{
alert("Errore con il primo indirizzo");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(address2, function (response)
{
try
{
if (!response || response.Status.code != 200)
{
alert("Errore con il secondo indirizzo");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
calculateDistance(nomi1,id1);
}
}
catch (e)
{
alert("Error in callback#2: "+ e);
}
});
}
}
catch (e)
{
alert("Error in callback#1: " + e);
}
});
}
catch (e)
{
alert("Error in functon: " + e);
}
}