javascript - Change the src of an image when the page loads -
i trying change src
of image , once changed remain on whole website.
<img id="profile" src="profilepicture.jpg"> <input type="text" name="image" id="imglink"> <button type="button" id="upload">upload</button>
jquery:
$(document).ready(function () { var link; $("#upload").click(function (){ link = $("#imglink").val(); $("#profile").prop('src', link); }); $("#profile").prop('src', link); });
as can see tried store link of image in global variable( var link ) , change src
of image inside function , change once again outside page loads image when it's loaded.
changing src
of image works once refresh page or go page , come back, image not changed more.
how can make remain changed on whole website?
normally on serverside since clientside solution works on 1 browser user using.
$(function () { //read localstorage var link = localstorage.getitem("myimage") || ""; //change "" default value $("#upload").click(function (){ link = $("#imglink").val(); $("#profile").prop('src', link); localstorage.setitem("myimage", link); //set localstorage new value. }); $("#profile").prop('src', link); });
Comments
Post a Comment