简体   繁体   English

Javascript代码在本地工作,但不能在服务器上工作

[英]Javascript code works locally, but not on server

I made a simple page on google maps, showing a bunch of pointers. 我在Google地图上做了一个简单的页面,显示了一堆指针。 When I try this code on my PC, it works without any problem. 当我在PC上尝试此代码时,它可以正常工作。

But, when I upload the files on my server, the Google Map dosen't show up, and I get the followings errors : 但是,当我在服务器上上传文件时,没有显示Google Map,并且出现以下错误:

SyntaxError: invalid property id variables.js:1 
SyntaxError: missing ; before statement fonctions.js:1 
SyntaxError: missing ; before statement villes.js:1 
ReferenceError: initialisation is not defined index.html:1

Any idea what I could have done wrong ? 知道我做错了什么吗? Thanks in advance :) 提前致谢 :)

My code : 我的代码:

index .html 索引.html

    <head>

        <link rel="stylesheet" href="style.css" />
        <title>Les dés de Monique</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false" ></script>
        <script type="text/javascript" src="./initialisation.js"></script>
        <script type="text/javascript" src="./variables.js"></script>
        <script type="text/javascript" src="./fonctions.js"></script>
            <script type="text/javascript" src="./villes.js"></script>


    </head>


    <body onload="initialisation()">

            <div id="panel">
                <input id="address" type="textbox" value="Bains de Bretagne, France">
                <input type="button" value="Ajouter" onclick="coderadresse()">
            </div>

        <div id="map_canvas" style="width: 100%; height: 100%;"></div>
    </body>



</html>

variables .js 变量.js

var geocoder = new google.maps.Geocoder();
var map;
var latlng = new google.maps.LatLng(46.1100, 2.1939);
var bounds;


var myOptions = 
{
    //Controles de l'utilisateur
    keyboardShorcuts: true,
    draggable: true,
    scrollwheel: true, 
    disableDoubleClickZoom: true,
    disableDefaultUI:true,


    //Affichage (ou non) des boutons
    navigationControl: true,
    mapTypeControl: true,
    zoomControl: true,
    panControl: true,
    overviewMapControl: false,
    rotateControl: false,
    scaleControl: false, 
    streetViewControl: false,
    center: latlng,


    mapTypeId: google.maps.MapTypeId.TERRAIN    
};

var styles = 
[
    {
            featureType: "localities",
            elementType: "labels",
            stylers: 
            [
                { 
                    visibility: "off" 
                }
            ]
    },
    {
            featureType: "road",
            stylers: 
            [
                { 
                    visibility: "off" 
                }
            ]
    }
];

villes .js 维尔斯.js

var villes = 
[
"Tirana",
"Berlin",
"Andorre-la-Vieille",
"Vienne",
"Bruxelles",
"Minsk",
"Sarajevo",
"Sofia",
"Nicosie",
"Zagreb",
"Copenhague",
"Madrid",
"Tallinn",
"Helsinki",
"Paris",
"Athènes",
"Budapest",
"Dublin",
"Reykjavik",
"Rome",
"Riga",
"Vaduz",
"Vilnius",
"Luxembourg",
"Skopje",
"La Valette",
"Chisinau",
"Monaco",
"Podgorica",
"Oslo",
"Amsterdam",
"Varsovie",
"Lisbonne",
"Prague",
"Bucarest",
"Londres",
"Moscou",
"Belgrade",
"Bratislava",
"Ljubljana",
"Stockholm",
"Berne",
"Kiev",
]

var coordonneesvilles = 
[
new google.maps.LatLng(41.33165, 19.83179999999993),
new google.maps.LatLng(52.519171, 13.406091199999992),
new google.maps.LatLng(42.50631740000001, 1.521835499999952),
new google.maps.LatLng(48.2081743, 16.37381890000006),
new google.maps.LatLng(50.8503396, 4.351710300000036),
new google.maps.LatLng(53.9, 27.56666670000004),
new google.maps.LatLng(43.8562586, 18.413076300000057),
new google.maps.LatLng(42.6978388, 23.32166970000003),
new google.maps.LatLng(35.1666667, 33.366666699999996),
new google.maps.LatLng(45.8130293, 15.977894900000024),
new google.maps.LatLng(55.6760968, 12.568337100000008),
new google.maps.LatLng(40.4167754, -3.7037901999999576),
new google.maps.LatLng(59.43696079999999, 24.75357459999998),
new google.maps.LatLng(60.17332440000001, 24.941024800000037),
new google.maps.LatLng(48.856614, 2.3522219000000177),
new google.maps.LatLng(37.9837155, 23.72930969999993),
new google.maps.LatLng(47.497912, 19.04023499999994),
new google.maps.LatLng(53.3498053, -6.260309699999993),
new google.maps.LatLng(64.13533799999999, -21.89521000000002),
new google.maps.LatLng(41.8929163, 12.482519899999943),
new google.maps.LatLng(56.9496487, 24.10518639999998),
new google.maps.LatLng(47.14136999999999, 9.520700000000033),
new google.maps.LatLng(54.6871555, 25.279651400000034),
new google.maps.LatLng(49.815273, 6.129583000000025),
new google.maps.LatLng(41.9973462, 21.42799560000003),
new google.maps.LatLng(35.89779, 14.51410599999997),
new google.maps.LatLng(47.02685899999999, 28.84155099999998),
new google.maps.LatLng(43.73841760000001, 7.42461579999997),
new google.maps.LatLng(42.442575, 19.26864599999999),
new google.maps.LatLng(59.9138688, 10.752245399999993),
new google.maps.LatLng(52.3702157, 4.895167899999933),
new google.maps.LatLng(52.2296756, 21.012228700000037),
new google.maps.LatLng(38.7252993, -9.150036399999976),
new google.maps.LatLng(50.0755381, 14.43780049999998),
new google.maps.LatLng(44.4325, 26.103888900000015),
new google.maps.LatLng(51.51121389999999, -0.11982439999997041),
new google.maps.LatLng(55.7512419, 37.6184217),
new google.maps.LatLng(44.8205556, 20.462222200000042),
new google.maps.LatLng(48.1458923, 17.107137299999977),
new google.maps.LatLng(46.05645089999999, 14.50807020000002),
new google.maps.LatLng(59.32893000000001, 18.064910000000054),
new google.maps.LatLng(46.9479222, 7.444608499999958),
new google.maps.LatLng(50.4501, 30.523400000000038),
]

initialisation .js 初始化.js

//Fonction d'initialisation de la carte, selon des valeurs précisées
function initialisation() 
{
   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   centrercarte();
   placermarqueurs();
}

fonctions .js 功能.js

function centrercarte()
{
    bounds = new google.maps.LatLngBounds();


    for (var i = 0 ; i < coordonneesvilles.length ; i++)
    {

        bounds.extend(coordonneesvilles[i]);
    }

    map.fitBounds(bounds);
}

function placermarqueurs()
{
    for (var i = 0 ; i < coordonneesvilles.length ; i++)
    {


            var optionsmarqueur =
            {
                position: coordonneesvilles[i],
                map: map,
                draggable: false,
                clickable:false,
                title:villes[i],
             }


         var marqueurville = new google.maps.Marker(optionsmarqueur);
    }
 }


function coderadresse() 
{
    var address = document.getElementById('address').value;

    geocoder.geocode( { 'address': address}, function(results, status)
    {
    if (status == google.maps.GeocoderStatus.OK) 
    {

      //Cette partie là devient inutile si on rafraichit la carte par redirections successives.  
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker(
      {
          map: map,
          position: results[0].geometry.location

      });


      envoyerdonnees(address, results[0].geometry.location);

     } 

     else 
     {
      alert('Désolé, le géocodage a échoué... ( ' + status + ')');
     } 
  });
}

function envoyerdonnees(nom, coordonnees)
{
    nom = ("\""+nom+"\", \n");
    coordonnees = ("new google.maps.LatLng"+coordonnees+", \n");    

    //redirection vers la fonction de traitement de fichier
    document.location.href=("./traitementfichier.php?nom="+nom+"&coordonnees="+coordonnees); 
 }

Check if the FTP-Upload had correct settings. 检查FTP上传是否设置正确。 Maybe you uploaded the files in "binary" mode instead of "ascii" mode. 也许您以“二进制”模式而不是“ ascii”模式上载了文件。

Another suggestion: the body "onload" is quite unsure, because the code will be executed when the parser his this line of code. 另一个建议:正文“ onload”是非常不确定的,因为当解析器解析此行代码时,将执行该代码。 So if you refer to the "map_canvas" the operation could fail. 因此,如果您引用“ map_canvas”,则该操作可能会失败。 On your PC the source code is loaded in milliseconds - so this doesn't hurt, but from your server (depending on your internet speed) this download could take too long. 在您的PC上,源代码的加载时间(以毫秒为单位)-因此不会受到影响,但是从您的服务器上下载(取决于您的Internet速度)可能会花费很长时间。

Try to implement jQuery as well with the following line: 尝试通过以下代码实现jQuery:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

An then use: 然后使用:

<script language="javascript">
$(document).ready(function() {
  initialisation()
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 在本地工作,但不在服务器上 - Javascript works locally but not on the server Javascript可在本地使用,但不能在带有niceEdit的服务器上使用 - Javascript works locally but not on server with niceEdit 处理代码在本地工作,但不能在javascript中工作 - Processing code works locally but not in javascript 对于在服务器上运行的代码(照相馆),未定义提供TypeError变量的Javascript。 相同的代码在本地工作 - Javascript giving a TypeError variable is undefined for code (photo gallery) run on server. Same code works locally Javascript可在本地运行,但在服务器上运行时会中断 - Javascript works locally but breaks when running on server Javascript在本地运行良好,但不在我的服务器上 - Javascript works great locally, but not on my server Javascript 在本地工作,但上传到服务器时出现错误 - Javascript works locally but bugs when uploaded to server HTML中的Javascript可在本地使用,但不能部署到服务器上 - Javascript in HTML works on locally but not when deployed to a server 我的JavaScript在本地可以运行,但是不能在服务器上运行吗? - my javascript works locally but not on the server why? Socket.io在服务器上返回Uncaught RangeError和文字javascript代码,但在本地运行良好 - Socket.io returning Uncaught RangeError and literal javascript code on server, but works fine locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM