English 中文(简体)
How do I detect if a javascript constructor already been run?
原标题:

I m using the jquery-tools tabs and carousel in a page I m working on. The carousel is embedded within a tab, and the tab is loaded in a beforeClick callback.

Normally, the carousel is initialized in the document ready function like so:

$(function() { 
        // initialize scrollable 
        $("div.scrollable").scrollable({ 
                size: 3, 
                clickable: true, 
                loop: false 
            }).mousewheel(); 

This doesn t work when the carousel is loaded via beforeClick, because it when the DOM is ready, this tab hasn t been loaded yet.

        $("ul.tabs").tabs("div.panes > div", {
          initialIndex: 0,
             onBeforeClick: function(event, i) {
                    // get the pane to be opened
                var pane = this.getPanes().eq(i);

                var src = this.getTabs().eq(i).attr("href");
                var langPattern = "/^" + getLang() +  / ;
                var forceReload = false;
                if (src.match(langPattern) == null)
                {
                  src = getLang() + "/" + src;
                  forceReload = true;
                }
                if (pane.is(":empty") || forceReload) {
                    // load it with a page specified in the tab s href attribute
                    pane.load(src);
                    // initialize scrollable - ONLY SHOULD BE DONE ONCE
                $("div.scrollable").scrollable({ 
                    size: 3, 
                    clickable: true, 
                    loop: false 
                }).mousewheel(); 
                }
            }
      }).history();;

My question is a two-parter:

First, is there an event I can add a handler to to initialize the scrollable when it is first loaded? Second, if there isn t, is there a way I can find out if the scrollable has been constructed yet?

The idea behind this code is to load localized html fragments based on the user s language selection. This is for a catalog on a CD, not online.

最佳回答

I solved my problem by attaching a callback to the load call:

pane.load(src, "", function(){
  $("div.scrollable").scrollable({ 
        size: 3, 
        clickable: true, 
        loop: false 
    }).mousewheel();    
});
问题回答

You might want to check out the jQuery live event. This event matches all current and future DOM matches.

EDIT 1 You want to bind to div.scrollable s onload event, correct?

$( div.scrollable ).live( load , function() {
    // etc.
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签