Skip to content

Instantly share code, notes, and snippets.

@andrii-marushchak
Last active August 31, 2022 00:13
Show Gist options
  • Save andrii-marushchak/64bd708b6a70e5579654e08b9454f6cd to your computer and use it in GitHub Desktop.
Save andrii-marushchak/64bd708b6a70e5579654e08b9454f6cd to your computer and use it in GitHub Desktop.
JS Snippets
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;
}
}
})
})
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
}
}
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
function forceDownloadFile(url, name) {
// Download File
var a = document.createElement("a");
document.body.appendChild(a);
a.href = url;
a.download = name;
a.click();
}
// 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)
})
function openInNewTab(href) {
Object.assign(document.createElement('a'), {
target: '_blank',
href: href,
}).click();
}
export {openInNewTab}
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;
}
}
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