Steps for loading and utilizing a CSS file when printing a specific div:

Is there a way to print a specific div while preserving its CSS styles? I am currently using both bootstrape.css and mycss.css for styling, but when I try to print the content, all CSS disappears. Here is my current implementation:

function printProfile(){            
    var contents = $("#profileContent").html();
    var frame1 = $('<iframe />');

    frame1[0].name = "frame1";
    frame1.css({ "position": "absolute", "top": "-1000000px" }); 
    $("body").append(frame1);

    var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;

    frameDoc.document.open();
    frameDoc.document.write('<html><head><title>Profile Contents</title>');
    frameDoc.document.write('<link href="css/bootstrape.min.css" rel="stylesheet" type="text/css" />');
    frameDoc.document.write('<link href="css/mycss.css" rel="stylesheet" type="text/css" />');
    frameDoc.document.write('</head><body>');
    frameDoc.document.write(contents);
    frameDoc.document.write('</body></html>');
    frameDoc.document.close();

    setTimeout(function () {
        window.frames["frame1"].focus();
        window.frames["frame1"].print();
        frame1.remove();
    }, 100);
}

I seem to be missing something in this code. What could it be?

Answer №1

attempt using media="print" when CSS is being loaded

<link href="css/mycss.css" rel="stylesheet" type="text/css" media="print"/>

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

I am interested in displaying the PDF ajax response within a unique modal window

With the use of ajax, I am able to retrieve PDF base64 data as a response. In this particular scenario, instead of displaying the PDF in a new window, is it possible to display it within a modal popup? Any suggestions on how this can be achieved? $.ajax ...

Use JSON data to populate a dynamic bar chart in Highcharts

As a beginner in parsing JSON objects to highcharts, I am trying to create a basic bar graph. I have managed to set up the title of the graph, but I am having trouble displaying the series that I want to show (count as series and qpAnswer as xAxis). Below ...

"Enhancing user experience with jQuery hover effects on table

This is an example of the HTML structure: <tr class="row-1 row-first"> <td class="col-1 col-first"> <div class="views-field views-field-field-logo-image"></div> <div class="views-field views-field-field-logo-title"> ...

Stopping package.json from updating dependencies

I am curious if there is a method to prevent the package.json file from automatically updating to the latest versions of dependencies it includes. The main reason for wanting to avoid these updates is that I rely on running specific scripts with certain l ...

Is it possible to update a current HTML value with a dynamically calculated value using HTML, CSS, or both?

While working on generating HTML to be passed back from a REST method, I encountered the following code snippet: StringBuilder builder = new StringBuilder(); builder.Append("<div class=\"row\">"); builder.Append("<div class=\"col-m ...

React Native NavigationStack encountering mismatched screen component types

Currently, I am in the process of developing an app using React Native and React Navigation, utilizing TypeScript. One important step I took was creating a type called RootStackParams for my routes as shown below: App.tsx export type RootStackParams = { ...

Concealing a hyperlink depending on the value chosen in a dropdown menu

Looking for guidance on how to use jQuery to read the current value of a drop-down list and hide a specific link based on that value. The drop-down list is for selecting the language/locale. For example, when "English" is selected, I want the link to be h ...

Tips for effectively handling 404 errors when loading directive templates in AngularJS

When working with an AngularJS directive, the templateUrl parameter is dynamically specified. 'templates/' + content_id + '.html' I am looking for a way to handle situations where the value of content_id may not be valid or result in ...

Bizarre text scenarios in JavaScript

I've come across something similar in certain libraries if ("testing" !== 'production') { "testing" !== 'production' ? warning(this instanceof Constructor, 'A React component is being called directly. Consider using ...

Using Javascript to retrieve form data from a separate file

I am struggling with my HTML and JavaScript files to collect data. Despite my efforts, the function is not executing properly. Below is the code I have been working on: HTML File : <form class="form-newsletter"> <div class="form-group"> ...

A recursive function in Javascript that utilizes promises

Utilizing HTTP calls to an embedded webserver, the function below is designed to delete files. The webserver accepts the DELETE verb for file deletion and works on empty folders as well. I brainstormed a function that recursively retrieves folder contents ...

Ensure the background image is repeated without any cropping or adjust it to make it symmetrical

Looking for this: https://i.sstatic.net/zz8lP.png Wanting this (also cropped but symmetrical): https://i.sstatic.net/Wrrlh.png What I currently have: https://i.sstatic.net/S1qtM.png div.row.separator div.col { height: 40px; width: 100%; padding: 0px ...

Resolving JavaScript animation delays in background tabs with instantaneous correction

Currently, I have a fiddle showcasing two pulsing animations that run at different timings. http://jsfiddle.net/JuFxn/16/ The code snippet for the pulse effect can be found within the fiddle itself. function fadeItIn() { var child; child = 4; setTimeou ...

How to retrieve HTML attribute using D3 techniques

Looking to iterate through all rect nodes in the code snippet below: d3.selectAll("svg g rect") .on('mouseover', function (d) { console.log(this); }); When Console.log is executed, the following is printed: <rect class="cls" na ...

Can NodeJS be updated using a script?

Is it possible to upgrade the nodejs version using a script? I am attempting to execute some code on play.js, which is a javascript IDE for iPad. However, the app does not provide a terminal option, and I am encountering errors due to the current node vers ...

The previous callback function is behaving strangely

<div class="actions_container" id="message_form_container"> ...<input type="text" id="message" />... </div> <div class="actions_container" id="coffee_form_container"> ...<input type="text" id="coffee_message" />... </div> ...

``Only Firefox supports jQuery animations, all other browsers fail to render them properly

This particular issue is a bit complex to articulate, so I'll begin by providing the code and then proceed to elaborate on the problem as best as I can. I'm assuming that you've already compared the results in Firefox and other browsers. He ...

Execute a PUT request within a Firebase Cloud Function database handler

I am working on syncing data between my server's database and Firebase realtime db. The first part, which involves syncing from my server to Firebase, is already complete. However, I am facing challenges with the second part - syncing data from Fireba ...

Creating a background overlay on top of an image background using styled-components in React: A step-by-step guide

I need help figuring out how to construct a unique component in react styled component that combines a background image with a black semi-transparent overlay. For visual reference, here is an example: https://i.sstatic.net/R6IBY.jpg ...

Unable to retrieve jQuery plugin value from ASP.NET textbox

I am struggling to retrieve the date value from the jQuery datepicker plugin that I have integrated into my ASP.NET web forms application. I have tried giving my textbox the id of my span id as suggested in the answers I found, but it still does not seem t ...