Show dynamic content html page

Show dynamic content html page

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.

id="default-content"class="dynamic-content">

  This is the default content

id="apples" class="dynamic-content">

  I like apples

id="oranges"class="dynamic-content">

  I like oranges

id="bananas"class="dynamic-content">

  I like bananas

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.

.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.

// Parse the URL parameter

functiongetParameterByName(name,url){

    if(!url)url= window.location.href;

    name=name.replace(/[\[\]]/g,"\\$&");

    varregex= newRegExp("[?&]"+name+"(=([^&#]*)|&|#|$)"),

        results=regex.exec(url);

    if (!results)returnnull;

    if(!results[2])return'';

    return decodeURIComponent(results[2].replace(/\+/g," "));

}

// Give the parameter a variable name

vardynamicContent= 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

$('#default-content').show();

}

});

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

Questions?

Email me, send me a tweet @jennamolby, or leave a comment

Receive my latest posts directly to your inbox

How do I display dynamic content in HTML?

How to Display Dynamic Content on a Page Using URL Parameters.
The HTML. Wrap each one of your dynamic content sections in a DIV and add a class called dynamic-content. ... .
The CSS. ... .
The JavaScript. ... .
Use jQuery to show/hide content. ... .
The full javaScript code. ... .
Questions?.

How do I change dynamic content in HTML?

The easiest way to modify the content of an HTML element is by using the innerHTML property..
The HTML document above contains an

element with id="id01".

We use the HTML DOM to get the element with id="id01".
A JavaScript changes the content ( innerHTML ) of that element to "New Heading".

How do I create a dynamic HTML page?

Follow these general steps to successfully design and create a dynamic website..
Design the page. A key step in designing any website—whether static or dynamic—is the visual design of the page. ... .
Create a source of dynamic content. ... .
Add dynamic content to a web page. ... .
Add server behaviors to a page. ... .
Test and debug the page..

How JavaScript can create dynamic HTML content?

JavaScript can be included in HTML pages, which creates the content of the page as dynamic. We can easily type the JavaScript code within the or tag of a HTML page. If we want to add the external source file of JavaScript, we can easily add using the attribute.