How to Get the Name of an Instance of a Class in Javascript

I was using JSONP (note that I prefer CORS but this was a special case where CORS wasn’t possible) and wanted an object that needed to call a method within itself. As you know, JSONP involves adding a script tag to the DOM which will return the JSON data encapsulated …

How to Wrap an Element Within a New Element Using Javascript

Here’s a simple way of wrapping an HTML fragment that exists in the DOM within another element, in this case a div: var wrapper = document.createElement(‘div’); wrapper.id = ‘the-wrapper’; wrapper.appendChild(document.querySelector(‘#the-html-fragment’)); At this point, the HTML fragment that was selected via the #the-html-fragment selector was removed from the DOM and attached …

How to Get the Current Script’s Parent in Javascript

The following is a method of getting the current script’s parent, which may be useful if, for example, you need to append HTML in the same location as the script: var scripts = document.getElementsByTagName(‘script’); parent = scripts[scripts.length – 1].parentNode; The above code maintains the assumption that the currently executing script …

Create an Extensible Object in Javascript

There’s a whole mess of ways to accomplish this (see this article). Obviously, YMMV, but I chose this route for one simple reason: it makes sense to me. Anyhow, here’s a method of creating an extensible object in Javascript. var theObject = function (config) {   this.exProperty = “”;   this.exFunction = …