2010-08-04 141 views
0

希望有人能夠解釋我的問題。基本上我只想執行一個代碼塊,如果某個DOM元素存在。如果是這樣,我然後執行幾個位和bobs然後調用一個函數。然而它抱怨函數沒有被定義,表明函數不在範圍內。下面是代碼:IF語句中的函數範圍

$(document).ready(function() 
     { 
     if ((document.getElementById("view<portlet:namespace/>:editSplash")!= null)) { 
     console.log("notifications scripted started"); 

     // hide loading box/ notify on body load 
     $('.ajaxErrorBox').hide(); 
     $('.loadingNotifications').hide(); 
     $('.notifyWindow').hide(); 
     getFeed(); 

     // set up refresh button for reloading feed 
     $('.refreshFeed').click(function() { 
     $('.notifyWindow').hide(); 
     $('.notifyWindow').empty(); 
     console.log("notifications clicked"); 
     getFeed(); 
     }); 

    // begin ajax call using jquery ajax object 

    function getFeed() 
    { 
    $('.notifyWindow').empty(); 
    console.log("ajax call for feed starting"); 
     $.ajax ({ 
     type: "GET", 
     url: "http://cw-pdevprt-05.tm-gnet.com:10040/notificationsweb/feed?username=uid=<%@ taglib uri="/WEB-INF/tld/engine.tld" prefix="wps" %><wps:user attribute="uid"/>", 
     dataType: "text/xml", 
     timeout: 10000, 
     success: parseXml  
     }); 
     }; 

     // show loading box on start of ajax call 

     $('.notifyWindow').ajaxStart(function() { 
     $('.refreshFeed').hide("fast"); 
     $('.notifyWindow').hide(); 
     $('.ajaxErrorBox').hide(); 
     $('.loadingNotifications').show("fast"); 

     }); 

     // hide loading box after ajax call has stopped 

     $('.notifyWindow').ajaxStop(function() { 
     $('.loadingNotifications').hide("slow"); 
     $('.refreshFeed').show("fast"); 

     }); 

     $('.notifyWindow').ajaxError(function() { 
     $('.loadingNotifications').hide("slow"); 
     $('.ajaxErrorBox').show("fast"); 
     $('.refreshFeed').show("fast"); 
     }); 

    // parse the feed/ xml file and append results to notifications div 

    function parseXml (xml) { 
     console.log("xml parsing begining");   
     if (jQuery.browser.msie) 
     { 
      var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
      xmlDoc.loadXML(xml); 
      xml = xmlDoc; 
     } 


    $(xml).find("entry").each(function() 
    { 

    var $item = $(this); 
    var title = $item.find("title").text(); 
    var linkN = $item.find("link").attr("href"); 
    var output = "<a href='" + linkN + "' target='_self'>" + title + "</a><br />"; 
    $(".notifyWindow").append($(output)).show(); 

    }); 
    } 



     } 
else { 
    console.log("notifications not available"); 
    return false; 
} 

}); 

如果DOM元素存在,然後嘗試調用getFeed函數「getFeed();」然而它回來未定義。如果任何人都可以闡明這一點,將不勝感激。

在此先感謝

+0

對元件的'id'是'視圖<門戶:命名空間/>:editSplash'?這不是有效的[名稱](http://www.w3.org/TR/html401/types.html#type-name)。 – kennytm 2010-08-04 08:52:30

+0

這是一個有效的名稱 - 我正在使用portlet,這是在jsf中檢索dom元素的名稱空間。歡呼聲 – jonnyhitek 2010-08-04 09:02:18

+0

你是什麼意思*回來undefined *? getFeed函數未定義? – 2010-08-04 09:15:58

回答

0

問題解決 - 我將我的職能移到if語句之外。我們生活和學習哈哈:-)

1

看來你在定義之前調用getFeed。嘗試將if語句移到函數定義之後。請注意,此行爲實際上是特定於實現的,因此某些瀏覽器可能以此方式工作,有些瀏覽器可能不會。

噢 - 真的嗎? view<portlet:namespace/>:editSplash爲一個ID?

+0

是的與JSF portlet的工作,因此,以確定DOM元素則需要例如使用portlet的名稱空間http://www.liferay.com/community/wiki/-/wiki/Main/Portlet+namespace;jsessionid=C2CD75F96318975D6BC68820A29D0B22.node-1 感謝您抽出時間來回答我的問題 - 我已經解決了porblem見下文 - 移動if語句之外的函數,因爲它們不在範圍內。 – jonnyhitek 2010-08-04 09:51:23

+0

這不是範圍本身 - 它是引起麻煩的執行順序。 – troelskn 2010-08-04 10:34:46