Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active July 7, 2020 03:34
Show Gist options
  • Save Offirmo/702cceecf85c0ef4ef1f92bbb677f4d7 to your computer and use it in GitHub Desktop.
Save Offirmo/702cceecf85c0ef4ef1f92bbb677f4d7 to your computer and use it in GitHub Desktop.
[useful browser snippets] #JavaScript #browser #growth #frontend
// debg
let DEBUG = false
try { // defensive!
DEBUG = DEBUG || !!window.localStorage.getItem(`${APP}.debug`)
} catch (e) { /* swallow */ }
if (DEBUG) console.info(`Hello from ${APP}`)
// prepare an url
// https://devdocs.io/javascript/global_objects/encodeuri
continueUrl = encodeURI(uri)
encodeURIComponent(...)
const data = {
var1: 'value1',
var2: 'value2'
};
const searchParams = new URLSearchParams(data);
// searchParams.toString() === 'var1=value1&var2=value2'
// current url
window.location.href
(new URL(window.location.href).searchParams.get('foo')
existing_search: [...current_search_params.keys()].reduce((acc, k) => {
acc[k] = current_search_params.get(k)
return acc
}, {}),
// Navigate
window.location = ...
// refresh
window.location.reload()
// open a new tab
window.open(url, '_blank').opener = null
if (window.confirm("Do you really want to leave?")) {
window.open("exit.html", "Thanks for Visiting!");
}
result = window.prompt(message, default)
window.alert("Hello world!")
https://stackoverflow.com/questions/7083693/detect-if-page-has-finished-loading
https://fokkezb.nl/2016/04/14/how-to-wait-for-a-javascript-variable-to-be-defined/
/////////////////
const CONTEXT_ID = 'FOO-1234'
const STORAGE_KEYS = {
last_display_ms: `${CONTEXT_ID}.last_admin_popup_display_s`
}
const storage = localStorage
const dateNowMs = Date.now()
const lastDisplayMs = Number(storage.getItem(STORAGE_KEYS.last_display_ms));
storage.setItem(STORAGE_KEYS.last_display_ms, dateNowMs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment