In this tutorial, I will show you how to show/hide dynamic content based on URL parameters for any web page. This solution uses HTML, CSS and JavaScript instead of backend coding, so you will be able to use it with Marketo, Pardot, or any other system that allows a bit of custom code.
The HTML
Wrap each one of your dynamic content sections in a DIV and add a class called dynamic-content. Also, give each DIV a unique ID. We will reference these later in the JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!-- Default Dynamic Section --> <div id="default-content" class="dynamic-content"> This is the default content </div> <!-- Dynamic Section 1 --> <div id="apples" class="dynamic-content"> I like apples </div> <!-- Dynamic Section 2 --> <div id="oranges" class="dynamic-content"> I like oranges </div> <!-- Dynamic Section 3 --> <div id="bananas" class="dynamic-content"> I like bananas </div> |
The CSS
There’s only one line of CSS needed to hide all the elements on the page since JavaScript will be used to show/hide the content.
1 2 3 |
.dynamic-content { display:none; } |
The JavaScript
This is the complicated part. First, we need to parse the URL and check for a specific parameter. For this example I will be using the parameter name “dc”, so in this case, my URL would look like this:
https://jennamolby.com/my-webpage?dc=mycontent
This is the piece of code to parse the URL. You can change “dc” to be whatever parameter name you want.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Parse the URL parameter function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } // Give the parameter a variable name var dynamicContent = getParameterByName('dc'); |
Use jQuery to show/hide content
To make things easier, we’ll use jQuery to show/hide the content, in conjuction with the javascript. You can add in as many conditions as you want, just make sure you always include default content just in case parameters are misspelled or not in the URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // Check if the URL parameter is apples if (dynamicContent == 'apples') { $('#apples').show(); } // Check if the URL parameter is oranges else if (dynamicContent == 'oranges') { $('#oranges').show(); } // Check if the URL parameter is bananas else if (dynamicContent == 'bananas') { $('#bananas').show(); } // Check if the URL parmeter is empty or not defined, display default content else { $('#default-content').show(); } }); </script> |
The full javaScript code
Here’s the full piece of javascript and jQuery code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script type="text/javascript"> // Parse the URL parameter function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } // Give the parameter a variable name var dynamicContent = getParameterByName('dc'); $(document).ready(function() { // Check if the URL parameter is apples if (dynamicContent == 'apples') { $('#apples').show(); } // Check if the URL parameter is oranges else if (dynamicContent == 'oranges') { $('#oranges').show(); } // Check if the URL parameter is bananas else if (dynamicContent == 'bananas') { $('#bananas').show(); } // Check if the URL parmeter is empty or not defined, display default content else { $('#default-content').show(); } }); </script> |
Questions?
Email me, send me a tweet @jennamolby, or leave a comment
Hi Jenna,
This post is quite interesting. I will give a try and will get back to you if I get struck somewhere.
Hi, i`m using dynamic keyword insertion ads(in adwords) and i already have de url with the keyword on it, but i also want to add that keyword as the DOM headline content.
Dynamically add/updated meta tags? You can use jQuery to do that. Here’s an example http://stackoverflow.com/questions/2568760/is-it-possible-to-use-javascript-to-change-the-meta-tags-of-the-page.
Hi Jenna,
How do I add spaces in the dynamic text? If it’s more than one word?
Thanks
Hello! Do you mean in the URL or in the text on the page? Since the script uses jQuery to show/hide HTML on the page, you should be able to use spaces as you normally would.
Thanks Jenna, Its Help me alot 🙂
I Owe You 😉
Thanks. I must be an aspiring nerd because I’m really excited about finding this!
Glad you found it useful, Jose!!