Re: [Rails] javascript hit and miss?

61 views
Skip to first unread message

Walter Lee Davis

unread,
Apr 25, 2018, 6:26:50 PM4/25/18
to rubyonra...@googlegroups.com
This would be a Turbolinks problem, very definitely. Turbolinks only refreshes the head of the page when you do a full reload. It's expected that your scripts will be compiled in the Asset Pipeline and will load once. Ever after, the body of the page is silently replaced via Ajax, thus speeding up the loading of your global libraries (by dint of only loading them on the first page load).

Now the problem comes when you have some code in your page body that your script needs to interact with, so you, being a pro, write proper unobtrusive JavaScript that wires up that connection later. Something like:

$(document).on('click', '.lightbox a', function(){
// do something with $(this), which will be the a (link) you clicked on
});

That should work without any modification, because it is lazy-loading the target at the moment the event fires, rather than wiring up the listener on page load.

I suspect that may be what your code is doing, so look through it for something that looks more like:

$(document).ready(function(){
$('.lightbox a').click(function(){
// do something with $(this), which will be the a (link) you clicked on
});
});

If you find that, either rewrite it to the lazy pattern I showed you first, or if it's too complex to untwist, change the $(document).ready bit to this:

$(document).on('load turbolinks:load', function(){
// ...
});

This will hook into the event that fires after Turbolinks refreshes the body of the page, so everything on the screen will be ready to be extended.

Walter

> On Apr 25, 2018, at 11:49 AM, Joe Guerra <jgu...@jginfosys.com> wrote:
>
> I've got some javascript on my rails site ( addthis toolbar & lightbox js ).
>
> They don't always load - I have to hit page refresh at times to get them to load.
>
> Would this be a javascript or rails problem?
>
>
> Thanks,
> Joe
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4dbf6afc-43b0-473b-b079-e293972e7f29%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Joe Guerra

unread,
Apr 30, 2018, 4:53:16 PM4/30/18
to Ruby on Rails: Talk
I think I found something that fixes the problem.

I've added it to my partial.

<script type="text/javascript">
if (window.addthis) {
window.addthis = null;
window._adr = null;
window._atc = null;
window._atd = null;
window._ate = null;
window._atr = null;
window._atw = null;
}
</script>



ok, you will have a minute to test it...

12345678


All of the product pages have a sharethis toolbar.

I don't know if it's working now, i tried something I found on google.

The other thing I noticed, now it thinks the page is not secure. 


Thanks,
Joe

Reply all
Reply to author
Forward
0 new messages