javascript - How to change background image dynamically depending on the url -
i'm trying change dynamically on page load, background depending on url are. doesn't work me.
var camera1 = "url1"; var camera2 = "url2"; var camera3 = "url3"; var currenturl = window.location; jquery(function($) { if(camera1 == currenturl) { $(".breadcrumb-right").css("background", "url(url1.jpg) !important;"); } else if (camera2 == currenturl) { $(".breadcrumb-right").css("background-image", "url(url2.jpg)"); } else if (camera3 == currenturl) { $(".breadcrumb-right").css("background-image", "url(url3.jpg)"); } });
maybe can improve code doing this:
const cameras = ['url1', 'url2', 'url3']; const currenturl = window.location.href; const el = $(".breadcrumb-right"); if (cameras.includes(currenturl)) { el.css("background-image", `url(${currenturl})`); }
Comments
Post a Comment