A Simple JavaScript to Lock Site Protected Content - Need Password To View

Simple Password Proctect


If you are able to view this post, then you would have seen the script in action.

Whether you desire a members-only section or a private area for selected individuals, this post will share a simple code to protect and restrict human access to a post. 

With this simple Script, you can lock access to a webpage or content to unauthorised viewers. This code works on any site, expect if your site doesn't use JavaScript.

I just love to concentrate my posts on blogger user since I'm one myself. Anything that works on blogger would work on any static site.



Implementing the Password Protection Script

For blogger users,

📌 Get to the post content or page you want to protect

📌 Switch to the HTML editor 

📌 In the HTML view of your page editor, copy and paste the following JavaScript code:


 <!--Password protect JS by sdavidprince-->    <script type="text/javascript"> 
var password = 'YourPassword';
password = prompt('A simple prompt- Please enter the password to enter this page:', '');
if (password != 'YourPassword') {
    location.href = 'WRONG_PASSWORD_REDIRECTION_URL_HERE';
}
</script>


🔖 Replace 'YourPassword' with the password you want to set for your page.

Additionally, replace 'WRONG_PASSWORD_REDIRECTION_URL_HERE' with the URL of the page where you want to redirect unauthorized users.

 You can also modify the prompt. All marked areas can be modified.

📌 Once you've implemented the password protection script, click on the "Save" button to save your changes. If it's a new or draft post, click on the "Publish" button to make your password-protected page live on your blog.

Implementing on Any Other Site

🥴 It's the same process for any site as long as you use a Code Editor 

Testing the Password Protection

To ensure that the password protection is working correctly, open a new browser window and visit your blog. 

Access the password-protected page, and you should see a prompt asking you to enter the password you set earlier. Once you enter the correct password, the page content will be visible.


That's It

Remember to regularly update and change your password to ensure the security of your content. Thank you for reading, and ...

... happy blogging!

Please write your comments or send a webmention
Webmention Notice

This site accepts Webmentions via webmention.io/www.sdavidprince.space/webmention. You can also reply to syndicated posts, and your responses may be backfed (posted back here) via brid.gy.

What does all these mean? Read more about it. Respect this and keep mentions relevant.

Dark/Light
Font Size
A-
A+

Additional Widgets

Translate

Explore: Navigation