Snippets

0
Reputation

Applying styles to selectors - insertRule()

Taken from https://davidwalsh.name/essential-javascript-functions

"...we can grab a NodeList from a selector (via document.querySelectorAll) and give each of them a style, but what's more efficient is setting that style to a selector (like you do in a stylesheet)... This is especially useful when working on a dynamic, AJAX-heavy site. If you set the style to a selector, you don't need to account for styling each element that may match that selector (now or in the future)."

0
Reputation

Get absolute URL

Taken from https://davidwalsh.name/essential-javascript-functions

"Getting an absolute URL from a variable string isn't as easy as you think. There's the URL constructor but it can act up if you don't provide the required arguments (which sometimes you can't). Here's a suave trick for getting an absolute URL from any string input... The "burn" element href handles any URL nonsense for you, providing a reliable absolute URL in return."

0
Reputation

Run once

Taken from https://davidwalsh.name/essential-javascript-functions

"There are times when you prefer a given functionality only happen once, similar to the way you'd use an onload event... ...The once function ensures a given function can only be called once, thus preventing duplicate initialization!"

0
Reputation

Debounce

Taken from https://davidwalsh.name/essential-javascript-functions

"The debounce function can be a game-changer when it comes to event-fueled performance. If you aren't using a debouncing function with a scroll, resize, key* event, you're probably doing it wrong. Here's a debounce function to keep your code efficient. The debounce function will not allow a callback to be used more than once per given time frame. This is especially important when assigning a callback function to frequently-firing events."

0
Reputation

Polling

Taken from https://davidwalsh.name/essential-javascript-functions

"...sometimes you don't get to plug into an event to signify a desired state -- if the event doesn't exist, you need to check for your desired state at intervals. Polling has long been useful on the web and will continue to be in the future!"

0
Reputation

$id() - Shortened document.getElementById()

Taken from https://websemantics.uk/archived/useful-javascript-functions/

"Saves repeatedly writing out document.getElementById('id'). Results are cached which speeds up repeat requests by 50%."

I really like this one, credit to Mike Foskett @ websemantics.uk

0
Reputation

Cookies

Taken from https://www.webhostingsecretrevealed.net/blog/featured-articles/15-cool-javascript-sample-snippets/

"This snippet is a little long but very useful, it will allow your site to store information on the viewer’s computer then read it at another point in time. This snippet can be used in many different ways to accomplish different tasks."

0
Reputation

jquery-3.3.1.min.js

Taken from jquery.com

"jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript."