I have a page that loads colorbox nodes on user click

visit node first time all works fine

close out node - and back to main page

click to view node again and javascript does not fire

any ideas that might help solve this issue?

Comments

dustinbrunson’s picture

I know that when the colorbox node is closed out that the javascript is still loaded, and therefore when the node is opened again there is not onload event fired for some reason.

I can't find a way to clear the javascript or know when the page is loaded to call a function to set things up again within the colorbox node window.

iLLin’s picture

Category: bug » support

You need to setup your javascript as a behavior.

dustinbrunson’s picture

Okay I am able to get the jquery into the colorbox with this code within my js file, however it is firing the doit() 3 times when the colorbox node opens.

(function($) {

Drupal.behaviors.module = {
attach: function (context, settings) {
doit();

}
};

})(jQuery);

iLLin’s picture

That is why you need to use .once() in your calls. It will prevent that.

$('#selector').once('myevent').click(function() {

});

Where your "myevent" is w/e you want to call it. This will allow you to setup multiple instances on a dom object if you need to by changing the myevent to a different one each time.

dustinbrunson’s picture

Thank you iLLin,

.once works except that I had more robust scripting going and could not use that for everything.

I was able to get it working with this implementation

Drupal.behaviors.mybehavior = {
attach: function (context, settings) {
$('#mybehavior', context).once('#mybehavior', function () {
// Apply the MyBehaviour effect to the elements only once.
dothefunction();
});
}
};

This way I could put all of my jquery within the dothefunction() and not it all works perfectly everytime. very cool, thanks for all your hard work and help.

iLLin’s picture

Status: Active » Closed (fixed)

No prob!

cancerian7’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

Hi Dennis,

Thanks for creating this module. I like it, but I am having the same issue with fb_social module.

I get the facebook comments box on the first view of colorbox node but on the second view it doesn't render. After, I refresh parent window it appears again on the colorbox node. Can you please tell me how to get rid of this issue? or make it compatible with fb_social module?

Thanks.

dustinbrunson’s picture

Hey Cancerian7, this is the code I use for custom javascript in my module. From the module I load this javascript file and then execute any code from within the doit function. since I don't know your specific problem, this is the best I can do to help. This is how I do it. It seems to attach the javascript directly to the module block so that it gets loaded again each time. not sure exactly why it works but it does :)

change our module-name for the name of your module - if its the fb_social module that is having problems you may need to look into its javascript file and change it to match this.

(function($) {

Drupal.behaviors.module-name = {
'attach': function(context) {

keystatus='no';
$('.block-module-name', context).once('block-module-name', function () {
// Apply the MyBehaviour effect to the elements only once.
doit();
});
}
}

function doit() {
// put your code here
}
})(jQuery);

cancerian7’s picture

Hi Dustin,

Thank you so much for looking into this. Here is the javascript which I found in the fb_social.module

  $output = '<div id="fb-root"></div>';
  $output .= "<script type=\"text/javascript\">
     window.fbAsyncInit = function() {
       FB.init({
         appId: " . drupal_to_js($appid) . ",
         status: true, 
         cookie: true,
         xfbml: true});
       
         jQuery.event.trigger('fb:init');    
     };
     (function() {
       var e = document.createElement('script'); 
       e.async = true;
       e.src = document.location.protocol + '//connect.facebook.net/" . $fb_locale . "/all.js';
       document.getElementById('fb-root').appendChild(e);
     }());
  </script>";

Here is the link to the module source code http://drupalcode.org/project/fb_social.git/blob/HEAD:/fb_social.module

Please help. Thanks

dustinbrunson’s picture

This is definitely beyond my expertise, but this may work for you

you need to make sure that there is a class name on the module that matches the name in this line

$('.fb_social', context).once('fb_social', function ()

or change the code below to match the classname on your module - for example if the module is #fb_social

then use this: $('#fb_social', context).once('fb_social', function ()

change the code in your file to this:

$output = '

';
$output .= "

(function($) { Drupal.behaviors.fb_social = { 'attach': function(context) { $('.fb_social', context).once('fb_social', function () { // Apply the MyBehaviour effect to the elements only once. doit(); }); } } function doit() { window.fbAsyncInit = function() { FB.init({ appId: " . drupal_to_js($appid) . ", status: true, cookie: true, xfbml: true}); jQuery.event.trigger('fb:init'); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/" . $fb_locale . "/all.js'; document.getElementById('fb-root').appendChild(e); }()); } })(jQuery)

";

if this doesn't work I am not sure I can be of anymore help.

cancerian7’s picture

Thanks for this, I guess you are getting close in solving this problem. Everything goes well after applying your code but it's printing the code with correct FB app id on the colorbox node and not rendering it.

I found another javascript in the source code of fb_social module.

print '<script src="http://connect.facebook.net/' . $fb_locale . '/all.js"></script>';
iLLin’s picture

Status: Active » Closed (works as designed)

It might be better to take this to the Facebook Social module page as they need to setup their stuff as behaviors so it gets reattached on each page load.

cancerian7’s picture

Ok sorry, I guess I have to run colorbox in an iframe now. Because your module is not working with many other modules like addtoany, sharethis, addthis etc. So, it's best to not create issues with other modules rather than just one.

Thanks for your time Dustin.