Printing does not work when using Window.print()

I am facing an error message "Stack over flow at line:0" in Internet Explorer when using the code below to print my Aspx web page. However, in Firefox, nothing seems to happen. Can anyone help me identify the issue?

<head>
 <script language="javascript" type="text/javascript">
        function print() {
            window.print();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
<div class="toolbar" style="width:400px">
            <ul>
<li>
                <img alt="" src="../../../CssImages/printer_128.png" id="ImgPrint" width="20px" style="cursor:pointer" onclick="print()"/>
                </li>
     </ul>

Answer №1

Your code snippet:

function print() {
    window.print(); // <-- refers to this custom function
}

When you define a custom function called print like this, it gets stored in the global object as window.print. This leads to a situation where you are actually calling the function itself, creating an infinite loop that causes an overflow error.

Since window.print is already pre-defined natively, there's no need for the custom function. Removing the function print() {...} declaration will allow the JavaScript to use the 'real' window.print method directly (as print is just a reference to window.print), resulting in the desired printing behavior.

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

What is the best way to obtain the id of a selected row using JavaScript?

I'm attempting to utilize the JavaScript function below to extract a specific cell value from a jqgrid upon clicking. Within the following function, #datagrid refers to the table where the jqgrid is located. $("#datagrid").click(function ...

Show the text from the chosen option using jQuery with a button

Hey everyone, I have a question regarding a coding issue I'm facing. In this problem, I am unable to display the text from the selected option when I click the order button. The desired result is to show the selected option that I choose and display i ...

Encrypting data using a straightforward Javascript method and then decrypting it using PHP with a

I am seeking a straightforward algorithm to transform a string (such as a URL) in a manner that disguises the original content without focusing on security measures. The encryption process will be carried out using JavaScript and then reversed by a PHP fun ...

Navigating through Controller.Response and the response from an ajax request

When making an ajax call, I want to display error messages upon success instead of on error. This will provide feedback on things that didn't work even though the call was successful. What specific code should I include in my controller to achieve a ...

Update the contents of a neighbor div within the same parent element using jQuery

Attempting to set the range slider value within the slider parent sibling var updateRangeSlider = function() { var slider = $('.range-slider'), range = $('.range- value = $('.range-slider__value'); sli ...

sending data to a PHP file via an AJAX request

I am attempting to send a JSON string and a variable through an AJAX call in order to edit the query in a PHP file. My code is structured as follows: queryData = { "data": { "data_string": { "data": "oil", "default_fiel ...

What are the steps to access an Alexa skill through a web browser?

I recently developed an Alexa skill for recipe recommendations. I am wondering if it is possible to have the skill open a browser on my phone and display the recipe when I say, "Alexa, send me the recipe"? The skill is working perfectly in the Alexa devel ...

What is preventing me from retrieving an ID value within an IF condition?

I'm trying to create a script that only allows devices with matching IP addresses, but I'm having trouble using an if statement. Whenever I include x.id inside the statement, it doesn't seem to work... any suggestions? <html> <sc ...

Creating dynamic EJS templates in JavaScript script

Currently, I am working with the Leaflet API to save user points (markers, circles, polygons) into a MongoDB database. I'm wondering if there is a more efficient and dynamic approach to writing a JavaScript script in an HTML page while retrieving data ...

Numerous instances of running JavaScript code

Having an issue with a JavaScript script. Using AJAX to load a subpage and append code to a div. $.ajax ({ type: "POST", url: "load/page/", dataType: "json", success : function(data) { $('.content').empty(); ...

The environmental variable remains undefined even after it has been established

I've been experimenting with setting my environment variable in the package.json file so that I can access it in my server.js file. Despite trying NODE_ENV=development, set NODE_ENV=development, cross-env NODE_ENV=development, and export NODE_ENV=deve ...

Trimming a Three.js Sprite to conform with the boundaries of its parent Object

Imagine a scenario where I have a Sprite, which was created and added as a child of an Object with a transparent material, like so: let mySprite = new THREE.Sprite(new SpriteMaterial({ map: myTexture })); mySprite.scale.set(2, 2, 1.0); mySprite.posit ...

Executing an Ajax SPARQL request in Firefox

I've been attempting to run an asynchronous Ajax sparql query on dbpedia using Firefox, but I encountered a strange issue that I can't seem to figure out. Surprisingly, the query works perfectly fine in Chrome, Edge, and Internet Explorer, but wh ...

Show a confirmation dialog box using bootbox that includes a link when the user selects OK

I am currently experimenting with integrating bootbox.js into a test page. However, I am facing an issue where the href tag in the hyperlink triggers regardless of whether the user selects the Cancel or OK button on the bootbox. Is there a way to include ...

How to sort Firebase real-time database by the child node with the highest number of

I am working on a database feature that allows users to 'like' comments on posts. Is there a way for me to sort the comments based on the number of likes they have? Although I am aware of using .orderByChild(), my issue lies in not having a sep ...

Adding new rows to an ng-repeat table in AngularJS with unique controls for each row, distinct from those

I have a table filled with JSON data using ng-repeat, similar to the example shown here: https://i.sstatic.net/Ffjuy.png Now, I am looking to insert new rows at the top of the table by clicking a button. However, these additional rows should contain edit ...

What's the most effective way to constrain focus within a Modal Component?

Currently working on a library of components in Angular 8, one of the components being a modal that displays a window dialog. The goal is to prevent focus from moving outside the modal so that users can only focus on the buttons inside by using the Tab but ...

troubleshooting problems with feathers.JS using the npm start command

After developing two separate feathersJS applications, I encountered a situation where running npm start resulted in two unique types of errors for each app. How can I go about resolving this issue? View image here https://i.stack.imgur.com/RrsGW.pnghtt ...

Implement the maskmoney library in your input fields

In the form below, I am automatically adding inputs using a JavaScript function like this: $('.Preco1').maskMoney({ decimal: '.', thousands: ' ', precision: 2 }); $('.Preco1').focus(); $('#sub').maskMon ...

Unable to access a nested JSON object that has a repeated name

I'm relatively new to working with JSON, so the issue I'm facing may be simple, but I haven't been able to find a similar problem on stackoverflow. Here's my question: My goal is to access a nested JSON object like: pizza.topping.ratin ...