aat-van-rees 4 years ago
Aat van Rees #Web Development

Aatventure Useful Javascript Snippets | 2020

Some useful javascripts snippets from around the web...

If you want to target all links with href that point to a different domain to open in a new tab, try this jQuery code snippet:

Code
$('a[href^="https://"]').not('a[href*=wp-mix]').attr('target','_blank');

Native Sharing

Code
function nativeShare() { shareURL = window.location.href; if (navigator.share) { navigator.share({ title: 'aatventure', text: 'Check this out!', url: shareURL, }) .then(() => console.log('Successful share')) .catch((error) => console.log('Error sharing', error)); } else // try sharing with whatsapp... currenturl = encodeURIComponent(window.location.href); url = "https://wa.me/?text="+currenturl; window.open(url,'_blank'); return false; }

On Orientation Change

Code
window.addEventListener("orientationchange", function() { console.log("The orientation of the screen is: " + screen.orientation); });

Prototype Functions

Code
String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.slice(1) } Array.prototype.contains = function(v) { for(var i = 0; i < this.length; i++) {if(this[i] === v) return true} return false } Array.prototype.unique = function() { var arr = []; for(var i = 0; i < this.length; i++) {if(!arr.contains(this[i])) {arr.push(this[i])}} return arr } Number.prototype.pad = function (len) { return (new Array(len+1).join("0") + this).slice(-len); }
Aat
van Rees