0

I want to fetch the background-image from this css how can i do it?

<div  class="zoomWindow" style="overflow: hidden; background-position: -174.967px -146.847px; text-align: center; background-color: rgb(255, 255, 255); width: 700px; height: 450px; float: left; background-size: 1100px 1500px; z-index: 100; border: 0px solid rgb(136, 136, 136); background-repeat: no-repeat; position: absolute; background-image: url(&quot;http://cdn.shopclues.com/images/detailed/45044/UX8A34631_1475562297.jpg&quot;); top: 200.4px; left: 623.5px; display: none;">&nbsp;</div>

my code open the new tab with url of background image BUT the domain name is still there i dont know why

Here is my code

var img = document.getElementsByClassName('zoomWindow');
console.log(img[0].style.backgroundImage.slice(4, -1));
var imagers =img[0].style.backgroundImage.slice(11, -2);
window.open(imagers);
Aman
  • 778
  • 2
  • 11
  • 35

4 Answers4

0

Using DOM:

document.getElementById('image_1').style.backgroundImage;

JQuery:

$('#image_1').css("background-image");

If you want just the url value, you will need to do one more thing, to strip the value url(&quot;http://cdn.shopclues.com/images/detailed/45044/UX8A34631_1475562297.jpg&quot;) of the url() function.

Martin Staufcik
  • 6,954
  • 4
  • 36
  • 58
  • how to get only image in new tab or popup? – Aman Dec 23 '16 at 07:45
  • to pass the value to another page, there are several ways: you could pass it as a parameter in url or via a cookie or save the value to the server and then retireve from the other page. But, what exactly are you trying to do, would not just creating a css for the other page (using the value of the image url) work for you? – Martin Staufcik Dec 23 '16 at 12:55
0

You can grab the value of the background-image property like so:

Pure JavaScript:

var imageSrc = document.getElementByClassName("zoomWindow");
var imageLocation = imageSrc.style.backgroundImage;

Or, if you have the jQuery library installed:

var imageLocation = $(".zoomWindow").css("background-image");

Hope this helps!

GROVER.
  • 3,376
  • 1
  • 16
  • 57
0

Try this

var one =$('.zoomWindow').css("background-image");
console.log(one);
ab29007
  • 7,448
  • 2
  • 16
  • 42
0

Here you have with the extraction of the URL applied:

$(function(){

  var $zoomWindow = $('.zoomWindow'),
      backgroundImage = $zoomWindow.css('background-image'),
      extraction = backgroundImage.match(/[^url('][^')]*/),
      url = extraction[0];

  console.log(url); // -> http://cdn.shopclues.com/images/detailed/45044/UX8A34631_1475562297.jpg

});
amedina
  • 2,552
  • 2
  • 15
  • 35