6

I was looking at the SO source code to see how they are doing the div on the right side bar that changes from relative to fixed position.

I saw that the SO JS library is pretty much all included into the page with this code below...

My question is how is the code included like this, is this something like the RequireJS or labJS javascript code that loads the files only when they are needed or something like that?

<script type="text/javascript">
StackExchange.using.setCacheBreakers({
    "js/prettify-full.js": "0324556b7bf7",
    "js/moderator.js": "a38ca3c6143d",
    "js/full-anon.js": "8fcefa158ad3",
    "js/full.js": "a168b3deac0f",
    "js/wmd.js": "688233b2af68",
    "js/third-party/jquery.autocomplete.min.js": "e5f01e97f7c3",
    "js/mobile.js": "97644ef9b7d4",
    "js/help.js": "7f83495f785a",
    "js/tageditor.js": "75954ba7b6f1",
    "js/tageditornew.js": "9d9998359a54",
    "js/inline-tag-editing.js": "364e12111b4b",
    "js/mathjax-editing.js": "a47e02eb0282",
    "js/revisions.js": "63c88065da1f"
});
</script>
4
  • 9
    Seems valid question to me Commented Nov 19, 2011 at 11:12
  • Have you already attempted to analyse the source code? jsbeautifier.org can be used to de-minify the code. Source code is available at cdn.sstatic.net/js/stub.js?v=e922af98260a. De-minified code: pastebin.com/W5ynGRNS Commented Nov 19, 2011 at 11:13
  • @Rob W Yes the code above is the Beautified version, I am still learning Javascript so I can somewhat follow it but I do not know enough to realize what or how it is including the files Commented Nov 19, 2011 at 11:16
  • @Sarfraz Hello, that is what I thought, I tried to make it appropriate Commented Nov 19, 2011 at 11:17

1 Answer 1

8

My question is how is the code included like this, is this something like the RequireJS or labJS javascript code that loads the files only when they are needed or something like that?

Yes, but not one of the two – it's a very tiny home-grown solution. The snippet you posted just lets the JavaScript know what cache breakers to use if including a file; it doesn't actually include them. That only happens when the file is actually needed.

I wrote a blog post that gives some insight into what's happening there.

Sign up to request clarification or add additional context in comments.

2 Comments

@balpha great! i'd like to use the "loadScript" function, is it copyrighted?
@shadesco: If anything, it's under the same license as jQuery (MIT/GPL at your choice), since that's where it comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.