Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created May 27, 2013 13:28
Show Gist options
  • Select an option

  • Save davestewart/5657050 to your computer and use it in GitHub Desktop.

Select an option

Save davestewart/5657050 to your computer and use it in GitHub Desktop.
jQuery script to defer loading of LiveReload script until the page is ready. Makes for faster initial page load - useful when working with JS templates and SPAs
$(function(){ $('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=2" />').appendTo('head'); });
@ThomasBurleson
Copy link
Copy Markdown

Type Gist is succinct but suffers from two (2) handicaps I believe:

  1. It is not easily usable.
  2. It does not provide any mechanism to let you know if the script has loaded or failed.

Based on your idea (above), I created a reusable $.deferredScript() method that uses jQuery Deferrred/Promises.

https://gist.github.com/ThomasBurleson/5674241

Any script can be easily loaded. Notification handlers are supported with Promises so script chaining is supported.
And the loaded script is immediately removed from the DOM after load and evaluation.

@davestewart
Copy link
Copy Markdown
Author

Jolly good!

This was more to make my development page loads a little snappier, when testing within a route on a Backbone SPA. Thanks for the comment, and I'll take a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment