Hide Elements Based On URL Text

Published: November 17, 2020

In this article we wanted to talk about how to hide individual HTML elements based off of text in the URL. This is helpful if you want to selectively disable elements of your web page by using a URL parameter like so:

https://example.com/?dontshowheader

We're going to do this in complete vanilla JavaScript, meaning that it should work with pretty much anything. Browser support is 100% for anything that runs JavaScript because of this. Once you have this snippet in your memory, it can be fairly helpful for iframe embeds, and more.

For us, we use this to hide our header and footer when adding content to a page via an iframe.

<script>
  
if (/URLSTRING/.test(window.location.href)) {
document.getElementById('ELEMENTTOHIDE').style.display = 'none';}
</script>

To get this working on your website, all you need to do is replace 2 things in this code snippet and then install it underneath the element that you want to hide.

The first thing is the text contained in the URL. If the URL contains the text in that snippet, then the element that it refers to will be hidden. If the URL does not contain the text, then nothing will happen.

In the example, we have “URLSTRING” (so if a url looked like this, https://example.com/?urlstring, and element would be hidden).

The next thing is the element you want to hide. This is selected by the element ID. Just replace “ELEMENTTOHIDE” with the ID of the element that you need to hide.

We use a URL parameter to selectively show and hide based off text in the URL because it makes no difference to the overall page, doesn't bring you to another subdirectory, and works off the bat.

Join Our Newsletter
Here's what you get: A once a week newsletter that contains original content, aggregated posts, WordPress deals, and anything else we think you'd like. We don't spam or sell your email address. Unsub at any time.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

isotropic black Friday deal finder

Search, sort, and filter through the best BFCM WordPress deals online.
BROWSE DEALS

Affiliate Disclosure: Some links on this website are affiliate links. We make commissions for purchases made through those links.

We only recommend items/tools that we've personally used and like.

Full Disclosure | FTC Statement

Join Our Newsletter
Over 4,000 Design Lovers and Problem Solvers.

Author

A relationship driven website creation company.

Connect

© ISOTROPIC, LLC 2016-2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram