$(document).ready(function() { // Variables var $codeSnippets = $('.code-example-body'), $nav = $('.navbar'), $body = $('body'), $window = $(window), navOffsetTop = $nav.offset().top, entityMap = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/' } function init() { $window.on('scroll', onScroll) $window.on('resize', resize) buildSnippets(); } function resize() { navOffsetTop = $nav.offset().top } function onScroll() { if(navOffsetTop < $window.scrollTop() && !$body.hasClass('has-docked-nav')) { $body.addClass('has-docked-nav') } if(navOffsetTop > $window.scrollTop() && $body.hasClass('has-docked-nav')) { $body.removeClass('has-docked-nav') } } function escapeHtml(string) { return String(string).replace(/[&<>"'\/]/g, function (s) { return entityMap[s]; }); } function buildSnippets() { $codeSnippets.each(function() { var newContent = escapeHtml($(this).html()) $(this).html(newContent) }) } init(); });