I am utilizing a piece of JavaScript code to change image sources on a WordPress website (leveraging the Elementor editor), which is based on a button click updating the URL with a specific string. For example, this process would yield the following:
Before Click: www.website.com/acoolpage/
After Click: www.website.com/acoolpage/?picture=ws10m
This HTML constructor creates the dimension of the image, but does not update the image source with the desired result after the button click, when the URL switches to www.website.com/acoolpage/?picture=ws10m
. What additional steps and/or edits are required? Thanks!
const urlParams = new URLSearchParams(window.location.search);
const pictureParam = urlParams.get('?picture=')
const pictureUrl =
switch (pictureParam) {
case 'ws10m':
return 'https://www.website.com/graphics/image_ws10m.png'
break
default:
return 'https://www.website.com/graphics/image_t2m.png'
break
}
<body>
<img src=pictureURL alt="Test" width="1920" height="1080">
</body>
2
Answers
You can use the
locationchange
event to detect if the URL has been changed by a button click.The code is as follows :
img src=pictureURL
is wishful thinkingYou likely meant to do this
Alternative for more versions