Posts

Showing posts with the label guest

Identifying the current <script> element

Guest blog post by Rodney Rehm You may find yourself in a situation requiring you to know the DOM context of the JavaScript that is currently executing. The DOM context of a running JavaScript is the <script> element that caused the JavaScript to be executed in the first place. With HTML5 the WhatWG introduced document.currentScript , which this article will use to explain simple techniques to make use of DOM context. What do I need the currentScript context for? The script you’re running may be in need of parameterization. It requires some sort of configuration, some data so it can do its job properly. Developers who’ve used RequireJS haven seen this before: <script src="scripts/require.js" data-main="js/main"></script> That’s a shorthand way of doing the following in JavaScript: <script src="scripts/require.js"></script> <script> require.config({ baseUrl: 'js/', deps: ['...