I have built a website using a website builder with a CMS similar to WordPress. I want to run a script before the page renders to see if I need to redirect a user to another page.
Currently, I have a JS script attached in the head tag, and the logic works fine when the conditions are met. However, sometimes the initial page may render for less than a second before it redirects to the updated path.
How can I avoid this page from rendering for this small timeframe? I can’t seem to find a way to do so. I tried to see if I could remove the whole body from the DOM and add it back once the function has run, but I wasn’t able to implement this successfully.
2
Answers
There’s a couple things you can do, the first one is using DOMContentLoaded which seems to be the most efficient:
This will wait for the document to be ready and then you can decide if you want to show content or redirect from in here.
If the above doesn’t suit your needs, you can also use CSS to display the page as none then implement your redirect:
This will never show the page body to users unless your condition is false. Hope this helps!
this may work for you :
some for jQuery