未定义 init() 的 Javascript 错误。如何消除这个错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6395437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript error of init() not defined . How to remove this error?
提问by IamH1kc
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="http://openlayers.org/api/2.10/OpenLayers.js" type="text/javascript">
var map, layer;
function init(){
map = new OpenLayers.Map( 'map', {controls: [
new OpenLayers.Control.Navigation({documentDrag: true}),
new OpenLayers.Control.PanZoom(),
new OpenLayers.Control.ArgParser(),
new OpenLayers.Control.Attribution()
]} );
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'} );
map.addLayer(layer);
map.zoomToMaxExtent();
}
</script>
</head>
<body onload="init()">
<h1 id="title">OpenLayers Document Drag Example</h1>
<div id="tags">
drag
</div>
<div id="shortdesc">Keep on dragging even when the mouse cursor moves outside of the map</div>
<div id="map" class="smallmap"></div>
<div id="docs">
<p>This example shows how to make a map draggable outside of the map itself.</p>
</div>
</body>
</html>
That is my html code with the javascript . My firebug throws up the error init() not defined . What can be the error ?
那是我的带有 javascript 的 html 代码。我的萤火虫抛出错误 init() not defined 。可能是什么错误?
回答by Frédéric Hamidi
Your <script>
element loads its content from an external resource (http://openlayers.org/api/2.10/OpenLayers.js
) since you specified that URL in its src
attribute.
您的<script>
元素从外部资源 ( http://openlayers.org/api/2.10/OpenLayers.js
)加载其内容,因为您在其src
属性中指定了该 URL 。
Therefore, the browser will ignore the actualcontent of the element, so init()
won't be defined.
因此,浏览器会忽略元素的实际内容,因此init()
不会被定义。
Try using two <script>
elements instead:
尝试改用两个<script>
元素:
<script src="http://openlayers.org/api/2.10/OpenLayers.js" type="text/javascript">
</script>
<script type="text/javascript">
var map, layer;
function init(){
map = new OpenLayers.Map( 'map', {controls: [
new OpenLayers.Control.Navigation({documentDrag: true}),
new OpenLayers.Control.PanZoom(),
new OpenLayers.Control.ArgParser(),
new OpenLayers.Control.Attribution()
]} );
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'} );
map.addLayer(layer);
map.zoomToMaxExtent();
}
</script>
回答by lokeshsk
This might help:
这可能有帮助:
http://openlayers.org/dev/examples/lite.html
http://openlayers.org/dev/examples/lite.html
It is the basic and simplest example of Openlayers.
它是 Openlayers 的基本和最简单的例子。