2016-01-17 41 views
0

我想獲得我的服務器上的圖像的自然大小,但我沒有得到我的圖像的真實來源。Javascript獲取錯誤的背景圖像src

我通過jQuery得到的來源:

.css('background-image').replace('url(','').replace(')',''); 

但當我這個URL存儲到img.src所以也沒有得到正確的圖像源。

我得到什麼:http://vatocc.net/%22http://vatocc.net/function/file/get/get_file.php?file=/secure/user_img/background_img/1265024466.jpg&purpose=view&format=false%22

真實鏈接:http://vatocc.net/function/file/get/get_file.php?file=/secure/user_img/background_img/1265024466.jpg&purpose=view&format=false

FIDDLE

UPDATE:

https://jsfiddle.net/7r5yu7ts/1/

+0

到所述第一圖像的鏈接斷開。 – Yass

+1

您不刪除引號,這就是爲什麼它不起作用,並且URL被認爲是相對的 – adeneo

+0

我無法複製。 – nicael

回答

1

嘗試:

var background = $('div').css('background-image'), 
imgPath = background.substring(background.lastIndexOf("(")+2,background.lastIndexOf(")")-1); 

全工作:

var background = $('div').css('background-image'), 
    imgPath = background.substring(background.lastIndexOf("(")+2,background.lastIndexOf(")")-1); 

var img = new Image; 
    img.src = imgPath; 
    $('i').html('Source: ' + img.src + 'Width: ' + img.width + ' Height: ' + img.height); 

https://jsfiddle.net/7r5yu7ts/3/