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>
Code language: HTML, XML (xml)
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.
Does not work for me. Do you have an example page for me? Regards, Monika
Hi! I know this is late but this code works perfectly. You just want to replace URLSTRING and the ID of the object only. Don't remove / / of URLSTRING.