const fs = require('fs');
// Read the file 'example.txt'
fs.readFile('example.txt', 'utf8', (error, data) => {
if (error) {
console.log("Error reading file:", error);
} else {
// Print file content to console
console.log("File content:");
console.log(data);
}
});
Category: Uncategorized
Reading from a File
const fs = require('fs');
// Read the file 'example.txt'
fs.readFile('example.txt', 'utf8', (error, data) => {
if (error) {
console.log("Error reading file:", error);
} else {
// Print file content to console
console.log("File content:");
console.log(data);
}
});
Disable Right Click
This code disables the browser’s right-click menu by listening for the contextmenu event, which occurs when the user right-clicks anywhere on the page. When the event is detected, event.preventDefault() stops the browser from showing its default menu, and an alert message is displayed to inform the user that right-clicking is disabled.
<!DOCTYPE html> <html> <body> <h2>Right Click is Disabled</h2> <script src="script.js"></script> </body> </html>
// Add event listener for right click
document.addEventListener("contextmenu", function(event) {
// Prevent default right-click menu
event.preventDefault();
// Show alert message
alert("Right click is disabled!");
});
Key Press Detector
This code listens for any key pressed on the keyboard using the keydown event listener attached to the document. When a key is pressed, JavaScript automatically provides an event object that contains information about the key, including which key was pressed. The code accesses event.key to get the key value and then updates the text inside the heading element using innerText to display the pressed key on the screen.
<!DOCTYPE html> <html> <body> <!-- Heading to show pressed key --> <h2 id="output">Press any key</h2> <!-- Link JavaScript --> <script src="key.js"></script> </body> </html>
// Add event listener for key press
document.addEventListener("keydown", function(event) {
// Display the key pressed by the user
document.getElementById("output").innerText =
"You pressed: " + event.key;
});