Reputation: 560
I am using jQuery to extract the URL of a background image and then store this in a cookie (to be used elsewhere on the site):
var pageImage = jQuery('#image > div').css("background-image");
This value is not being passed to the cookie, and using console.log(pageImage) I can see that the value being stored is:
url("https://www.example.com/images/image1.jpg")
I need to store just the URL string in the cookie i.e:
https://www.example.com/images/image1.jpg
Any help as to how to remove url("
and ")
using jQuery (in the cleanest way possible)?
Upvotes: 0
Views: 30
Reputation: 2999
You can read more about cookies here https://www.w3schools.com/js/js_cookies.asp
var str = `url("https://www.example.com/images/image1.jpg")`;
var res_url = str.substring(5,str.length-2);
console.log(res_url);
// and then
// document.cookie = res_url;
But generally using cookie and specially for such a task is not recommended. try using Web Storage instead :
HTML web storage provides two objects for storing data on the client:
window.sessionStorage - stores data for one session (data is lost
when the browser tab is closed)
// Store
localStorage.setItem("desire-background", "url-of-background-image");
// Retrieve
document.getElementById("result")
.style.backgroundImage = localStorage.getItem("desire-background");
Upvotes: 2