2012-05-30 177 views
0

我的用戶最近告訴我,默認頁面加載比以前慢得多。 我認爲它是因爲地圖在頁面加載時立即加載。 有沒有辦法延遲它,直到點擊這裏點擊按鈕?在加載頁面中執行javascript


根據我到目前爲止收到的答案,我決定從外部URL調用地圖。 但是在加載頁面上的Javascript沒有執行。 鋤頭我能解決這個問題嗎?

謝謝!

這就是我所說的外部頁面。

$(function() { 

    // if the function argument is given to overlay, 
    // it is assumed to be the onBeforeLoad event listener 
    $("a[rel]").overlay({ 

     mask: 'darkred', 
     effect: 'apple', 

     onBeforeLoad: function() { 

      // grab wrapper element inside content 
      var wrap = this.getOverlay().find(".contentWrap"); 

      // load the page specified in the trigger 
      wrap.load(this.getTrigger().attr("href")); 
     } 

    }); 
}); 
<!-- external page is given in the href attribute (as it should be) --> 
<a href="default.cs.asp?Process=ViewCheckinMap" rel="#overlay" style="text-decoration:none"> 
    <!-- remember that you can use any element inside the trigger --> 
    <button type="button">Show external page in overlay</button> 
</a> 

這是什麼不執行。

<script> 
     function initialize() { 
     var data = <%=Data%>; 
     var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453); 

     var map = new google.maps.Map(document.getElementById('map'), { 
      zoom: 2, 
      center: center, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     var markers = []; 
     for (var i = 0; i < data.users.length; i++) { 
      var location = data.users[i]; 
      var latLng = new google.maps.LatLng(location.latitude, 
       location.longitude); 
      var marker = new google.maps.Marker({ 
      position: latLng 
      }); 
      markers.push(marker); 
     } 
     var markerCluster = new MarkerClusterer(map, markers); 
     } 
     google.maps.event.addDomListener(window, 'click', initialize); 
</script> 

回答

2

關於你的改變的問題:

刪除google.maps.event.addDomListener(window, 'click', initialize);

變化wrap.load(this.getTrigger().attr("href"));到:

wrap.load(this.getTrigger().attr("href"), function(){initialize();}); 

這應該初始化您的地圖,加載外部JavaScript後。

沒有測試:(

+0

錯誤:未定義初始化。 – Efe

+0

比你在JavaScript的其餘部分有錯誤。您的頂級js代碼可能不會顯示您的腳本的開始。結尾'});'表明這一點。 應該在加載內容之前定義函數'initialize'。 –

+0

如果我簡單地從另一個頁面加載它,如上面提到的agriboz那樣。你爲什麼認爲這不起作用? – Efe

1

這意味着你需要一個外部頁面加載ajax。

要做到這一點,新的HTML頁面。並將其命名爲map.html。在map.html頁面上放上你的谷歌地圖代碼。

爲您的默認頁面<a href="map.htm" rel="#overlay">Click here to open the map</a>

之後,你的地圖將被加載。 contentWrap使用ajax方法的div。

<!-- overlayed element --> 
<div class="apple_overlay" id="overlay"> 
    <!-- the external content is loaded inside this tag (map.html) --> 
    <div class="contentWrap"></div> 
</div> 

的CSS的.contentWrap

/* container for external content. uses vertical scrollbar, if needed */ 
div.contentWrap { 
height: 400px; // it is the same height of the #map div 
overflow-y:auto; 
} 
+0

這個沒有工作。主要頁面加載犯規從新頁地圖。沒有錯誤,任何想法,爲什麼? – Efe

+0

您可以提供站點鏈接? – agriboz

+0

你有這個'

'在默認頁面 – agriboz