Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use jquery to manipulate css to change background image with base64 string

Tags:

jquery

css

Here's the problem. i've a base64 image string which is dynamically retrieved from database via ajax.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvsAAAJECAYAAACIHevuAAAgAElEQVR4nO3d

after retrieving the string i want to set it as the background image of a div

<div id="dragBox"></div>
like image 761
root Avatar asked Aug 01 '11 09:08

root


2 Answers

try:

$('#dragBox').css('background-image', 'url(' + base64_string + ')');

basically, you just put the base64 inside url().

more information:"Data URI scheme" Wiki

like image 77
Shea Avatar answered Oct 13 '22 07:10

Shea


You can simply place your image data directly into the url parameter of the background property in CSS.

#dragBox {
background: url(data:image/png;base64,iv[...]);
}

Here's an example: http://jsfiddle.net/uyrjs/

like image 30
Jamie Dixon Avatar answered Oct 13 '22 07:10

Jamie Dixon