Run code on every page that hasn't been visited before
if (localStorage.getItem(window.location.href) ===null) {// run code}localStorage.setItem(window.location.href,"visited");
localStorage & sessionStorage
// All these same options work with sessionStorage also// gets the value of itemlocalStorage.getItem("Name");// sets the value of itemlocalStorage.setItem("Name","Value");// removes itemlocalStorage.removeItem("Name");// removes all local storagelocalStorage.clear();// Check if item has been set beforeif (localStorage.getItem("Name") !==null) {// item is set} else {// item is not set}// Check if item equals a certain valueif (localStorage.getItem("Name") ==="Your Name") {// item matches} else {// item does not match}
Query Parameters
Example url: https://www.your-website.com/?username=John&hobby=Sports
// store paramsconstparams=newURLSearchParams(window.location.search);// check if the url contains a certain parameterif (params.has("username")) {// run code}// check the value of a certain parameterif (params.get("username") ==="John") {// run code}
Control videos
// get videolet video =$(".my-section").find("video");// pause videovideo[0].pause();// play videovideo[0].play();// restart videovideo[0].currentTime =0;// mute videovideo.prop('muted',true);// unmute videovideo.prop('muted',false);// enable loopvideo.prop('loop',true);// disable loopvideo.prop('loop',false);// on video end (loop must be disabled for this to run)video.on('ended',function() {// run code});// on playvideo.on('play',function() {// run code});// on pausevideo.on('pause',function() {// run code});// on timeupdatevideo.on('timeupdate',function() {$(".my-text").text(this.currentTime);});// on volumechangevideo.on('volumechange',function() {$(".my-text").text(this.volume);});
AJAX
On click of ".your-link", get ".item-on-next-page" and add it to current page.
$(".your-link").on("click",function (e) {e.preventDefault();let link =$(this).attr("href");$.ajax({ url: link,success:function (response) {let element =$(response).find(".item-on-next-page");$("body").append(element); },complete:function () {console.log("Got It"); } });});
On page load, get item from certain page and bring it to current page
// include this code on 404 page to add cms navbar to 404 pagelet link =window.location.origin; // homepage url$.ajax({ url: link,success:function (response) {let element =$(response).find(".your-navbar");$("body").prepend(element); },complete:function () {console.log("Got It"); }});
Insert text into field while triggering any events attached to that field