2010-08-04 143 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