cancel

Insert HTML based on URL - Javascript

A simple but effective way to show different HTML based on the URL using javascript. This code is stackable which allows multiple variations of content based on the URL.

For example: Surface a differnet header image for emails and QR codes using tracking codes.

if (location.href=='URL1') {
  document.getElementById('DIV_ID').innerHTML = 'HTML CONTENT';
}
if (location.href=='URL2') {   document.getElementById('DIV_ID').innerHTML = 'HTML CONTENT'; } else {   document.getElementById('DIV_ID').innerHTML = 'HTML CONTENT'; }

Instructions

  1. Copy and Paste code above into your webpage. Make sure you add script tags.
  2. Enter URL's into URL1 and URL2. (do not remove single quotes)
  3. Enter the ID of the Div area you wish to place HTML. (do not remove single quotes)
  4. Write HTML variations into the HTML CONTENT spots for URL1, URL2 and else. (do not remove single quotes)
  5. Save and Preview

This will serve HTML based on the URL1, URL2 or everything else.