javascript hit and miss?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

javascript hit and miss?

Joe Guerra
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: javascript hit and miss?

Walter Lee Davis
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 <[hidden email]> 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/641792E0-3DA1-4DE3-9CA0-D2AF5F325B75%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: javascript hit and miss?

Joe Guerra
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



On Wednesday, April 25, 2018 at 6:26:50 PM UTC-4, Walter Lee Davis wrote:
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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="wuxFKwsuBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jgu...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="wuxFKwsuBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="wuxFKwsuBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/4dbf6afc-43b0-473b-b079-e293972e7f29%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/4dbf6afc-43b0-473b-b079-e293972e7f29%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/4dbf6afc-43b0-473b-b079-e293972e7f29%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/4dbf6afc-43b0-473b-b079-e293972e7f29%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/74555ab5-df20-4314-b8c7-ec6cf269821b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.