2011-03-19 54 views
0

作爲我的絕對java新手,我折磨自己到了我的位置。但是,我做到了。而且,我不能更快樂!但是......我有最後一個最後一個問題。與Smarty和Google Maps API的Javascript

我正在使用Smarty。 Smarty爲我加載動態JavaScript以加載我的地​​圖標記。

我需要包含我的頁眉和頁腳.tpl文件。我的header.tpl有<head><body>,但我的地圖代碼取決於<head><body>是它的位置。

代碼的一部分是問題。

我絕對不知道如何執行我的JavaScript沒有這個「onload」代碼。有人可以給我一個小費嗎?

我真正需要的是知道如何啓動我的JavaScript代碼,而不依賴於<body>標籤。

這是我的代碼,非常感謝你提前!

{* 

{include file="header.tpl"} 
<h3 class="titlehdr">Book Buyers - Accounts Map</h3> 
{if $userlevel == 5} 
    <p>Admin User</p> 
{/if} 
Hello, {$name}! 
<br><br> 

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
    html { height: 50% } 
    body { height: 50%; margin: 0px; padding: 0px } 
    #map_canvas { height: 50% } 
</style> 
*} 



<br><br> 

<script type="text/javascript" 
    src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 
{foreach from=$getMap key=k item=v} 
    var buyer{$k} = new google.maps.LatLng 
    {foreach from=$v key=k item=latlng} 
     {if $k == "lat"} 
      ({$latlng} , 
     {else} 
      {$latlng}); 
     {/if} 
    {/foreach} 
{/foreach} 

var marker; 
var map; 

function initialize() { 
    var mapOptions = { 
    zoom: 5, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    center: warehouse 
    }; 

    map = new google.maps.Map(document.getElementById("map_canvas"), 
     mapOptions); 

    marker = new google.maps.Marker({ 
    map:map, 
    draggable:false, 
    animation: google.maps.Animation.DROP, 
    position: buyer5 
    }); 
{foreach from=$getMap key=k item=v} 
    marker{$k} = new google.maps.Marker({ 
    map:map, 
    draggable:false, 
    animation: google.maps.Animation.DROP, 
    position: buyer{$k} 
    }); 
{/foreach} 

} 

</script> 

<body onload="initialize()"> 
    <div id="map_canvas" style="width:70%; height:70%"></div> 


{include file="footer.tpl"} 

回答

0

您可以使用JQuery來做到這一點,您只需在<script></script>標籤中調用該代碼即可。將文檔加載到瀏覽器中並準備就緒時,您可以調用代碼。

<script type="text/javascript"> 
    $(document).ready(function(){ 

     //Your code supposed to be called, goes here. 

    }); 
</script> 

雖然即使在文檔的任何部分寫道:<script>標籤的作品,如果你用「偉大」的瀏覽器,你知道我的意思;-)。但是,將這些標籤放在頁面上的任意位置並不是一個好習慣,因爲這可能是一個兼容性問題。因此,請將代碼保存在您網頁的<head></head>標籤內。

對於JQuery,您可以從其網站下載最新的穩定版本,或者在<head>標記中簡單添加以下行。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>