Last active
August 31, 2022 00:13
-
-
Save andrii-marushchak/64bd708b6a70e5579654e08b9454f6cd to your computer and use it in GitHub Desktop.
JS Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const links = document.querySelectorAll('a[href^="#"]:not(.modal-toggle)') | |
links.forEach((nodeElement) => { | |
nodeElement.addEventListener('click', function (e) { | |
e.preventDefault() | |
const href = this.getAttribute('href') | |
if (href === '#') { | |
return false; | |
} else { | |
const section = document.querySelector(href) | |
if (section) { | |
let offset = section.getBoundingClientRect().top + window.scrollY | |
const wpAdminBar = document.getElementById('wpadminbar') | |
if (wpAdminBar) { | |
offset -= wpAdminBar.offsetHeight | |
} | |
window.scrollTo({top: offset, behavior: 'smooth'}) | |
/* | |
$('html, body').animate({ | |
scrollTop: offset | |
}, 500); | |
*/ | |
} else { | |
return false; | |
} | |
} | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function changeHashWithoutScrolling(hash) { | |
if (history.replaceState) { | |
window.history.replaceState(null, null, '#' + hash); | |
} else { | |
const id = hash.replace(/^.*#/, '') | |
const elem = document.getElementById(id) | |
elem.id = `${id}-tmp` | |
window.location.hash = hash | |
elem.id = id | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CopyToClipboard( val ){ | |
var hiddenClipboard = $('#_hiddenClipboard_'); | |
if(!hiddenClipboard.length){ | |
$('body').append('<textarea style="position:absolute;top: -9999px;" id="_hiddenClipboard_"></textarea>'); | |
hiddenClipboard = $('#_hiddenClipboard_'); | |
} | |
hiddenClipboard.html(val); | |
hiddenClipboard.select(); | |
document.execCommand('copy'); | |
document.getSelection().removeAllRanges(); | |
hiddenClipboard.remove(); | |
} | |
or check - https://github.com/lgarron/clipboard-polyfill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function forceDownloadFile(url, name) { | |
// Download File | |
var a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.href = url; | |
a.download = name; | |
a.click(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!localStorage.getItem('referrer')) { | |
localStorage.setItem('referrer', document.referrer); | |
} else { | |
if (document.referrer.indexOf(location.host) == -1) { | |
localStorage.setItem('referrer', document.referrer); | |
} | |
} | |
localStorage.getItem('referrer') ?? document.referrer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set the observer options | |
const options = { | |
root: null, | |
rootMargin: '0px', | |
threshold: 0.10 // 10% of obj 'seen' | |
} | |
// Links with sections #id in href | |
const navLinks = document.querySelectorAll('.scroller-section__nav-list li a') | |
const sections = document.querySelectorAll('.sub-section') | |
// Create an Observer | |
const observer = new IntersectionObserver(entries => { | |
entries.forEach(entry => { | |
if (entry.isIntersecting) { | |
entry.target.classList.add('observed') | |
navLinks.forEach(link => { | |
if (link.hash === `#${entry.target.id}`) { | |
link.classList.add('active') | |
} else { | |
link.classList.remove('active') | |
} | |
}) | |
} | |
}); | |
}, options) | |
// Observe sections | |
sections.forEach(section => { | |
observer.observe(section) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function openInNewTab(href) { | |
Object.assign(document.createElement('a'), { | |
target: '_blank', | |
href: href, | |
}).click(); | |
} | |
export {openInNewTab} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function random(min, max, int = false) { | |
if (max == null) { max = min; min = 0; } | |
if (min > max) { let tmp = min; min = max; max = tmp; } | |
let result = min + (max - min) * Math.random(); | |
if(int){ | |
return ~~(result); | |
}else{ | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let url = new URL('http://demourl.com/path?id=100&topic=main'); // window.location.href | |
let search_params = url.searchParams; | |
// set | update | replace | |
search_params.set('id', '101'); | |
// updated ( many params with same name ) | |
search_params.append('ip', '127.0.0.1'); | |
// delete | |
search_params.delete('id'); | |
url.search = search_params.toString(); | |
let new_url = url.toString(); | |
// Update current url | |
window.history.replaceState({}, document.title, new_url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment