BlueRiver89
BlueRiver89

Reputation: 77

How do I load an image from an image path stored in a cookie?

On one page I stored the path to an image in a cookie. On the second page, I use the following:

<br /><b>Photo:</b> " + $.cookie("image");

It just displays the full link on the next page. It is the exact link to the image, so that's good. But how can I make it display an image instead of the path?

Obviously this won't work:

<img src="$.cookie("image");

But I'm at a loss.

Upvotes: 1

Views: 780

Answers (2)

Hamed Ali Khan
Hamed Ali Khan

Reputation: 1118

Add id or class to image tag and using jQuery to change the image src

HTML

<img class="cookieImage" src="" ... >

jQuery

$('.cookieImage').attr('src', $.cookie("image"));

Upvotes: 1

GoodSp33d
GoodSp33d

Reputation: 6282

You need to create the inline HTML through script:

Suppose yout HTML is structured like:

<br /><b>Photo:</b><div id="image_container"></div>

Then you could use below script to load that image:

var imagePath = $.cookie("image");
$('#image_container').html('<img src="'+ imagePath +'" width=100 height=100/>');

Or you could alter the Src attribute of the Image tag.

<br /><b>Photo:</b><img id="image_container"></img>
$("#image_container").attr('src', imagePath);

JSFiddle

Upvotes: 1

Related Questions