Break your String down starting from it's innermost nesting, and each time consider where it will be interpreted and therefore how it needs to be encoded.
<script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
This HTML will be inside a String, escape quotes and backslashes and go up a level in nesting
window.jQuery || document.write("<script src=\'http://code.jquery.com/jquery-1.10.2.min.js\'></script>");
This script will be inside HTML, escape tags and go up a level in nesting
<script type="text/javascript">window.jQuery || document.write("<"+"script src=\'http://code.jquery.com/jquery-1.10.2.min.js\'><"+"/script>");</script>
This HTML will be inside a String, escape quotes and backslashes and go up a level in nesting
document.write('<script type=\"text/javascript\">window.jQuery || document.write(\"<\"+\"script src=\\\'http://code.jquery.com/jquery-1.10.2.min.js\\\'><\"+\"/script>\");</script>');
This will be loaded as an external script so we're done.
I chose to break tags using a "<"+"tag>..<"+"/tag>"
style because I find it easier to escape.