I am loading a form into a page via javascript and I only want to show this form when the url contains a specific expression like: /home.
The form loads for the main page and all sub pages and should only be shown for a sub page containing the above expression, while hidden for all other cases.
I am relatively new to javascript and this problem really strikes me, so any help would be welcome and greatly appreciated.
Best,
Christian
Any help to get started is welcome
2
Answers
You can check the URL of the page and then display the form based on the specific conditional expression. I’ll share a simple example using JavaScript for your trouble.
HTML
JS
This code uses the
window.location.pathname
property to get the current page’s URL path. It then checks if this path contains the specified expression ("/home") using the includes method. Depending on the result, it sets the display style property of the form container to either "block" (to show) or "none" (to hide).You can hide element when url does not include ‘/home’ like this