Tips for stopping the Print dialog box in Mozilla when using the CTRL + P shortcut

When I press CTRL + P on my view, a JavaScript code is triggered. The code works perfectly on all browsers except Mozilla; I cannot seem to block the Print Dialogue on that browser. Is there something wrong with my code?

I have been attempting to implement my own logic for printing using Ctrl + P (especially when multiple i frames are present on the page). After spending hours trying to achieve this, I resorted to blocking the print dialogue on Mozilla. However, it seems that Mozilla still displays the Print dialogue unlike IE 11, Edge, and Chrome.

$(document).bind("keyup keydown", function (e) {
if (e.ctrlKey && e.keyCode == 80) {
    var browser = navigator.userAgent.toLowerCase();
    if(browser.indexOf('firefox') > -1)
   {
       return false;
   }}
}

My goal is to prevent the default behavior of CTRL + P on Mozilla and run my custom script instead.

This issue pertains to an MVC web app, in case that information is relevant.

Answer №1

In my opinion, I prefer using the following code snippet:

$(document).on("keydown keyup", function (e) {
    if (e.ctrlKey && e.keyCode === 80) {
       e.preventDefault();
    }
});

This way, it ensures consistent behavior across all browsers by always preventing default - it's interesting that other browsers don't require this.

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

Acquiring POST parameters within Laravel's Controller from JavaScript or Vue transmission

I am trying to send Form data from a Vue component to a Laravel API using the POST method. Although Laravel is returning a successful response, I am encountering difficulty in handling the POST data within the Laravel controller. Below is the code for th ...

Utilize Bootstrap v4.6 to control the visibility of navigation pills based on specific times of the day using JavaScript

My webpage contains two nav-pills that I want to hide during certain hours of the day and then make visible again later on. However, there is a third nav-pill that should remain unaffected and be visible at all times. function showNavPill() { let now = ne ...

Refreshing a Next.js page results in a 404 error

I've set up a Next.js page called page.js that can be accessed through the URL http://localhost:3000/page. However, I have a need to access this page through a different URL, namely http://localhost:3000/my-page. To achieve this, I decided to utilize ...

Adding fresh elements to the state array in React Native

I am experiencing an issue with the code provided below: import React, { Component } from "react"; import { SafeAreaView } from 'react-navigation' import {Keyboard, Text, View, TextInput, TouchableWithoutFeedback, Alert, KeyboardAvoidingView, St ...

What is the best way to change the HTML content of just one specific div element?

I am facing what seems like a straightforward issue - I have various pages, each with their unique 'content' section and page navigation located at the bottom. Rather than creating multiple HTML files with redundant head, body, and footer code fo ...

Revising Global Variables and States in React

Recently delving into React and tackling a project. I find myself needing to manage a counter as a global variable and modify its value within a component. I initialized this counter using the useState hook as const [currentMaxRow, setRow] = useState(3) ...

Using Observables to assign values received from API calls to each element during loop iterations

As I loop through using a foreach loop, I need to call functions that will make async API calls and return values to be rendered in the HTML. The first function getCurrentValue() will return the currentTemperatureRef which should then be assigned to recei ...

Building a custom search modal using RenderPartial in Yii

I'm currently working on developing a modal box that will enable users to filter and search through a grid. The main objective is to allow users to retrieve information from one database while simultaneously completing a form in another database. Alt ...

Is the parameter retrieval method correct or erroneous?

I am looking to retrieve data by clicking a link. Here are the links available: <a class="note" id="' . $data["p_id"] . '" value="1" href="javascript:void(0)">+1</a> <a class="note" id="' . $data["p_id"] . '" value="-1" ...

Modifying the style of the HTML placeholder tag

One approach I am taking involves utilizing a placeholder element within my search box input: <input id="searchBarTextbox" type="search" class="k-textbox" placeholder="Search For..." /> My challenge now is to enhance the aesthetics of the placehold ...

What is the best way to pass a variable value from a JavaScript function to a Python function within a Django framework

My goal is to use HTML and JavaScript to scan a QR code in nurse_home.html. I have successfully scanned and viewed the content of the QR code on the website, but I am facing an issue with POSTing the QR code output represented by <output type='POST ...

Transfer the contents of one array into the center of another array using JavaScript

I have been looking for answers here, but I can only find solutions for different programming languages. Currently, I am dealing with 2 Uint8 typed arrays. var arr1 = [0,0,0]; var arr2 = [0,1,2,3,4,5,6,7,8,9]; My goal is to replace the contents of arr2 ...

Check for my variable in the redux state before proceeding

Currently, I am creating connection and registration screens, with a profile button on the bottom tab bar. The objective is for the user to be directed to their profile page if they are logged in (user data stored in Redux), or to a landing screen with log ...

Struggling with a character entity in Javascript? Learn how to escape it and avoid any display issues (such as showing

document.title = ("welcome &rarr; farewell"); Trying to display the arrow symbol "→" but it's not showing correctly. Any tips on how to properly escape it? ...

Transforming a file with a node module that lacks a .js class into a browseable format

I am currently working on browserifying my module. One of the dependencies I have is on this https://www.npmjs.com/package/chilkat_win32. It is present in my node_modules folder and here is how the structure looks: https://i.stack.imgur.com/uw9Pg.png Upo ...

How can a string be transformed into a JavaScript Object without using JSON?

I have a good grasp on parsing a valid JSON string using JSON.parse('{"key" : "value"}'). But what happens when dealing with a valid JS object, but invalid JSON, such as: JSON.parse("{ key : 'value'}")? The outcome of this example is a ...

PHP form button refuses to coordinate

My form page was functioning well, submitting form data to an ajax request without issues. However, it suddenly stopped working and no error messages are being logged. I tried identifying the problem by reviewing recent changes made to the file, but the on ...

Issue with Angular 5 - Deselect all checkboxes not reflecting in the UI

I am currently working on integrating a reset button into a Reactive form in Angular 5. The reset functionality works flawlessly for all form fields, except for the dynamically created multiple checkboxes. Although it seems like the reset operation is hap ...

What is the best way to convert a circular JSON object to a string

Is there a way to stringify a complex JSON object without encountering the "Converting circular structure to JSON" error? I also need its parser. I am facing issues every time I try to use JSON.stringify and encounter the "Converting circular structure to ...

Keep the div element steady as you scroll to the bottom

Currently, I have a collapsible side navigation whose height is unknown, with a div underneath it. The goal is for the div to change its position to 'fixed' when scrolled to its bottom. I have successfully achieved this functionality; however, I ...