On the homepage of my website I want to add a redirect in Javascript so that when…
Jump to turn (6)
- 1On the homepage of my website I want to add a redirect in Javascript so that whe
- 2Ok, but I want the redirect to only happen when the page is loaded without an an
- 3Ok, how can I add this code to my WordPress site?
- 4Can I instead create a small plugin that adds this code?
- 5Ok, that worked. I need one more modification to my site: I am using the Site ti
- 6Ok, but can you split this function in two separate functions, so one handles th
- User
On the homepage of my website I want to add a redirect in Javascript so that when someone visits the homepage, they are sent to a specific inner anchor called #home. What is the javascript code I need to add there?
- Assistant
You can achieve the desired behavior by adding the following JavaScript code to your homepage. You can either include it in your existing script file or add it as an inline script within the
<head>or<body>tags of your HTML file.javascriptdocument.addEventListener('DOMContentLoaded', function() { window.location.hash = 'home'; });This code snippet listens for the
DOMContentLoadedevent, which is fired when the initial HTML document has been completely loaded and parsed. Once this event is triggered, the script will update the URL's hash to '#home', effectively navigating to the specified inner anchor.