What I'm trying to do is getting a JS parameter to replace an image SRC with a custom URL using a URL parameter.
Example of parameter:
https://example.com?i=https://example.org/img.jpg
What I'm trying to do is getting a JS parameter to replace an image SRC with a custom URL using a URL parameter.
Example of parameter:
https://example.com?i=https://example.org/img.jpg
There are a few ways to do this and I hope this gets you started (I have not tested this code):
function getUrlVars () {
let vars = {};
const p = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,k,v) {
vars[k] = v;
});
return vars;
}
const src = getUrlVars('i');
const img = document.getElementById('image-id-here');
img.src = src;
The getUrlVars is a generic function I often use. It's likely more complicated than what you need.
A word of waring; you should do the extra work/research to validate the content of the URL parameter before inserting into into your page.