Is it possible to make an HTML page permanent and uneditable?

Is it possible to secure my HTML file from being edited or modified, especially in a web browser?

Discover innovative techniques and gain deeper insights into web development and website design.

Interested in learning more about HTML, CSS, JavaScript, PHP, C#, SQL, Python, and other programming languages.

Answer №1

To prevent your friends from tampering with the site's HTML through inspecting the page, you can implement a clever solution using a MutationObserver.

Take a look at this example to see how you can be alerted when changes are made to the page's HTML:

window.addEventListener('load', function() {
  const observer = new MutationObserver(function() {
    console.log('Stop right there!');
  })

  observer.observe(document.body, {
    characterData: true,
    childList: true,
    attributes: false,
    subtree: true
  });
})
<h2>Go ahead and try editing!</h2>
<p>Lists can contain nested lists:</p>

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>

If you wish to undo any modifications made to the HTML, you could save the initial body's innerHTML on load and then revert it upon change.

While this can be amusing, please refrain from using it on an actual website as completely replacing the innerHTML may result in breaking your site. This is because all elements are essentially destroyed and recreated, leading to loss of previously attached event listeners.

window.addEventListener('load', function() {
  const startHTML = document.body.innerHTML;
  const observer = new MutationObserver(function() {
    if(document.body.innerHTML != startHTML){
      alert('Nice attempt, but no.');
      document.body.innerHTML = startHTML;
    }
  })

  observer.observe(document.body, {
    characterData: true,
    childList: true,
    attributes: false,
    subtree: true
  });
})
<h2>Go ahead and try editing!</h2>
<p>Lists can contain nested lists:</p>

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>

Answer №2

To prevent users from easily accessing your page source code, you can consider disabling the right-click function. Preventing Right-Click: While this method may not completely stop skilled individuals from altering your HTML, it can act as a deterrent for casual users. By incorporating a script that disables the right-click context menu, you can increase the difficulty for users to inspect or modify the page's underlying code.

 html
   <script>
     document.addEventListener('contextmenu', function (e) {
       e.preventDefault();
     });
   </script>

It is important to note that this approach is not foolproof and may be circumvented with relative ease.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP file upload error: Angular JS form submission issue

I am currently working on creating an upload method using Angular and PHP. Here is what I have come up with so far... HTML <form class="well" enctype="multipart/form-data"> <div class="form-group"> <label for ...

Leveraging the Firebase email trigger extension with JavaScript

Recently, I integrated the email trigger extension for Firebase. Below is the function I am using: firestore .collection("profiledata") .doc(user.uid) .update({ accountStatus: "active", }) .then(() => { // At this p ...

Stop jQuery popups from wrapping text when resizing the browser window

Whenever a link is clicked, a jQuery popup appears. The popup functions properly in terms of opening and closing, but I would like to ensure that its position remains fixed when resizing the browser window to avoid any wrapping issues. Typically, I use a ...

Is there a way to change an ISO 8601 date into the format '/Date(1525687010053)/' using JavaScript?

Is there a way to convert a date value that is formatted as 9999-12-31T00:00:00Z to the format /Date(1525687010053)/ using javascript? I tried implementing the following code, but it doesn't seem to be working: var datevalue = '9999-12-31T00:00 ...

Validation is only effective for the initial row and fails to apply to subsequent rows. This may be due to the necessity of including the return true/false statement in my validation process

HTML PHP <div align="center"><input type="text" class="form-control" id="<?php echo $i;?>" name="r2<?php echo $i+1;?>" onblur="mvalidate2()" style="text-align: center; bo ...

Capturing a res.send(404) error in ExpressJS during production: Tips and tricks

Can middleware handle errors like 404 and 500 when they are returned this way? exports.index = function(req, res) { res.send(404); } In production, I would want to display a custom missing page for these errors. However, my error handler middleware doe ...

Loading SVG images in advance

I am in possession of around one hundred simple SVG images, distributed among five different image folders. These images are currently retrieved on demand when they need to be displayed, which generally works well but occasionally results in flickering tha ...

The port is not defined in the express when running with the command "node ."

After going through the tutorial mentioned here, everything was smooth sailing until I reached the part where I had to run the server: https://www.digitalocean.com/community/tutorials/setting-up-a-node-project-with-typescript Attempting to execute the cod ...

The adhesive navigation bar isn't adhering

I'm having issues with making my navbar inside a header sticky over the whole page. Despite trying various solutions like removing overflow, adjusting the flexbox, and setting a specific height on the parent element, I still can't get it to work. ...

Upgrading from ng-router to ui-router in the Angular-fullstack application

issue 1: url:/home, templateUrl: 'index.html is appearing twice. problem 2: views: templateUrl: 'views/partials/main.html is not visible at all. What am I doing wrong? How can I effectively incorporate ui-router into yeoman's angular-fulls ...

Utilizing Selenium for web scraping on Google Maps consumes an excessive amount of data

Scraping travel times from Google Maps is a task I have successfully accomplished. The code below scrapes travel times between 1 million random points in Tehran utilizing multiprocessing for efficiency. You can run the code provided in a terminal environme ...

Ways to Halt observable.timer in Angular 2

As I work on Angular2's Component, I am currently implementing the following functions: export class MypageEditComponent { ngOnInit() { this.timer = Observable.timer(100, 100); this.timer.subscribe(t => { this.setFormData(); } ...

Integrating a PHP function within an ECHO statement

Here is a code snippet I'm currently using in my Wordpress: function wpstudio_doctype() { $content = '<!DOCTYPE html>' . "\n"; $content .= '<html ' . language_attributes() . '>'; echo apply_filte ...

Retrieve an array object containing specific properties using axios

Currently, I am retrieving JSON data from an API call using axios and displaying it through Vue. This snippet shows the JSON Object logged in the console: 0: category_id: "categ1" item_name: "item1" price: 100 stock: 155 1: c ...

Resolving Uncaught TypeError in React when trying to read properties of undefined (specifically 'map')

As a newcomer to React and API usage, I am facing difficulty resolving the error below. My goal is to showcase a carousel of images using React.js but encountered this issue: Error message: Popular.jsx:26 Uncaught TypeError: Cannot read properties of unde ...

Angular index.html file can include a conditional script

I am currently working on an Angular project, where the index.html serves as the main entry point for the application, just like in any other Angular project. This file contains important links and configurations. Within the HTML code snippet below, you w ...

Loading templates (partials) in Angular.js on the fly

Is there a way to dynamically load templates into an Angular app based on a parameter within a ng-foreach loop? <body ng-app="MyApp" ng-controller="ExampleController as example"> <div ng-repeat="item in example.items" class="someClass" ng-swi ...

What is the process for accessing the theme spacing unit in MUI 5?

In previous iterations of MUI, accessing the theme spacing unit was possible through theme.spacing.unit, with a default output of 8. However, this property has been removed in MUI 5. I am having trouble finding documentation on how to access the theme sp ...

Guide to inheriting and overriding text styles

I am looking for a way to customize the style of all h6 elements within reactjs/material-ui, without using themes. I have attempted the code below, among other methods: const useStyles = makeStyles({ someClass: { h6: { fontSize: &qu ...

Guide to fetching the title image and content using AJAX in PHP

How can I effectively display the title, image, and content data? Take a look at the code snippet below: view.php $id = $_REQUEST['edit_literature_id']; $literature = $_REQUEST['literatureID']; $module = $_REQUEST[&ap ...