Skip to content

Instantly share code, notes, and snippets.

@RobertCam
Last active September 7, 2023 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertCam/d2005e43dadb15f93c9dfb7610ba501c to your computer and use it in GitHub Desktop.
Save RobertCam/d2005e43dadb15f93c9dfb7610ba501c to your computer and use it in GitHub Desktop.
Javascript to change the background of an Unbounce page section based on a "background" URL parameter
<script>
/*
Unbounce Community :: Tips & Scripts :: Dynamically Change Background Image with URL Parameters
TS:0002-04-045
***********************
Do not remove this section. It helps our team track useage of external workarounds.
*/
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
var background = getURLParameter('background');
if (background) {
document.getElementById("lp-pom-block-8").style.backgroundImage = 'url('+background+')'; // add the ID of the section you would like the change the background for
};
</script>
@DPUK44
Copy link

DPUK44 commented Oct 10, 2019

Hey man, is there anyway I can get this working for images and not just background images?

@websitepernikahan
Copy link

Hi @RobertCam, I really interested on your topic. It work at your webpage, But I failed when implementing it to my website builder (mobirise). Can you give a a similar page, but without tutorial, so I can copy paste all the section code (without filtering) to my website builder. I've tried to copy paste your current tutorial page, but elimated tutorial section is really not an easy task for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment