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

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -