⚠ The Black Friday 2020 WordPress Deals List Is Live!
Check It Out!

Hide Elements Based On URL Text

Authored By: James LePage
Published On: November 17, 2020

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

amazon elementor E book

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 list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
James LePage is the founder of Isotropic Design, a digital agency that builds WordPress websites. I read, write and design constantly!

Social Share

Request A WordPress Tutorial
Crafting Stunning Digital Appearances & Assets Out Of New York.
Get In Touch
Syracuse, NY | Charlotte, NC | New York, NY
© 2020 Isotropic, LLC
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram