Leveraging the power of jQuery and vanilla JavaScript to create a pop-up print window for a Div element within a CMS platform that does not support media query stylesheets

I have been grappling with this particular issue for weeks now, seeking guidance from previous inquiries here. Yet, despite the helpful hints, I am still struggling to find a viable solution.

My website features a scrolling sidebar measuring approximately 350x320 pixels, and I am eager to implement a dedicated Print function for it. The sidebar is encapsulated within a Div element. I have already added a print button inside the sidebar that triggers a new print window.

An interesting quirk of this challenge is that our CMS does not support "media-query" specific stylesheets, thus ruling out the option of a Print stylesheet!

The crux of my difficulty lies in understanding the syntax of what I believe is jquery's "$css()" function, which needs to be inserted within the callback function on the page.

<script type="text/javascript">
    $(document).ready(function() {

    $("a.printText").click(function(){
        w=window.open('','_blank','width=1024,height=768,scrollbars=1,location=0,menubar=0')
        w.document.write('<link rel="stylesheet" type="text/css" href="css/print.css">');
        w.document.write("<h2>{$node.name}</h2>");
        w.document.write("<h3>{$node.data_map.dek.content.output.output_text}</h3>" + "<p>By {attribute_view_gui attribute=$node.data_map.instructors}</p>");
        w.document.write($('#descIntro').html());       
        /*w.print();
        w.close();*/
    });
    });
</script>    

The line "w.document.write('');" proved ineffective, serving as a workaround that failed to deliver results.

Below are the styles that must be seamlessly integrated into the aforementioned callback:

body {
color: #000;
font: 400 12px/19px Palatino, Georgia, serif;
line-height: 1.5em;
margin: 15px 20px;
}

h2 {
color: #3d3d3d;
}
h3 {
color: #3d3d3d;
margin-bottom: 1px;
padding-bottom: 0;
}

p {
font-size: 1.1em;
margin: 0 0 4px 0;
padding: 0;
}

ul, ol {
margin: 3px 0 3px 20px;
}
.sidebarText {
background-color: #dfdfdf;
border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px;
margin: 7px auto;
padding: 1px 12px 8px;
width: 500px;
}

.sidebarText + h3 {
padding-top: 0;
margin-top: 0;
}

This may seem like a simple matter, but given its specificity, I am optimistic that someone possesses the quick solution right at their fingertips.

Thank you for your assistance!

Answer №1

An effective method involves creating a fake print document that references its unique stylesheet (often referred to as the print stylesheet). This fake document is then loaded into a hidden iframe, where jQuery can be used to transfer the contents into the fake document's body for printing purposes.

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

Is there a method in JavaScript to access the object to which a function was originally bound?

I have a curiosity about making the code below function properly, capturing the logging as instructed in the comments. function somePeculiar(func) { var funcThis = undefined; // Instead of undefined, how can we access // ...

Is there a way to maintain the selected position on the drop-down menu for users?

Whenever I choose an option from the drop-down field in my form, it automatically jumps back to the top of the page. Just to clarify, I have a drop-down menu at the top of the page and several input fields below. Users need to scroll down to reach the dro ...

Ways to make a chosen row stand out in an *ngFor loop?

Struggling to find a solution within Angular2 for setting a css class when selecting a row. I want to achieve this without relying on jQuery. <table class="table table-bordered table-condensed table-hover"> <thead> <tr> ...

Learn how to implement a feature in your chat application that allows users to reply to specific messages, similar to Skype or WhatsApp, using

I am currently working on creating a chatbox for both mobile and desktop websites. However, I have encountered an obstacle in implementing a specific message reply feature similar to Skype and WhatsApp. In this feature, the user can click on the reply butt ...

Attempting to send JSON data using a button in a PHP script

I'm facing an issue with trying to add new data to my JSON file using jQuery by inputting values, but nothing seems to happen. I apologize for what may seem like a simple question and appreciate any assistance. As someone who is new to programming, I ...

What is the best way to display a Base64 image in a server-side DataTable?

This HTML code is designed to load server-side data into a table. The data is retrieved from the controller using AJAX requests. <script type="text/template" id="tablescript"> <td><%=Name%></td> <td><%=PhoneNumber%> ...

I am noticing that my popover is causing my page to shift when I click it. It is expanding the width of my page more than I would

Upon clicking the user id popover on my page, it expands the page width instead of adjusting within the page boundaries. This is the issue: https://i.stack.imgur.com/EqaMo.png There's a small white space present that I want to eliminate. When the po ...

Using jQuery to send a GET or POST request directly to a PHP function

I'm seeking some advice. I currently have a search.php file that uses jQuery to retrieve search values input by users. What I'm curious about is if it's feasible to use a jQuery .get JavaScript function to directly call specific PHP function ...

Restrict the occurrence of a specific element in the array to a maximum of X times

Functionality: A feature in this program will randomly display elements from an array. Each element can only be shown a certain number of times. Issue: I am unsure how to limit the number of times each element in the array is displayed. Currently, all ...

In Javascript, you can quickly update other variables with just a one-line conditional statement

Is there a way to simplify this code into one line without using an if-else statement? For example, updating all variables when a specific condition is met? const dogStatus = present ? "bark" : "beep"; const catStatus = present ? "meow" : "meep"; const f ...

View real-time data in Vuejs 3 as it executes

I am currently working on a form that populates a table with data retrieved from a Laravel API. I am using Vue.js 3 and Composition API to build my entire application. When the button is clicked, I want the table to be filled with data from the form. The b ...

accessing PHP array elements within JSON

How can I access an array of arrays returned from PHP in JSON format? This is the PHP array: $cities = array(); while($row = mysql_fetch_array($result)){ $cityRow= array('cityNo'=>$row['city_no'], 'cityName'=>$row[ ...

Enhance your Select options using Ajax and Jquery by updating based on the value of another Select option

I'm struggling to understand how to dynamically update the options in a second select box based on the selection made in the first one. To achieve this, I am aiming to utilize a predetermined JSON object for the available choices in the second select ...

The issue arising where the callback function fails to execute after making an AJAX POST request

Within my function, I have an AJAX call that I want to make reusable. When the AJAX call is successful, I need to callback a function. var ajaxPostCall = function(data , url ,callback){ // Return the $.ajax promise $.ajax({ data: data, dataType: ...

Developing an interactive menu for mobile devices utilizing a combination of JSON, HTML, JavaScript, and CSS

Creating a custom dynamic menu for mobile platforms using HTML, JavaScript, and CSS with a JSON object downloaded from the server. Not relying on libraries like jQuery. I've come across advice stating that "document.write" should not be used in event ...

Refreshing the Datatable does not work when the page state is set to true

Currently, my data table is equipped with a feature that saves the state of the current page. var tbl = $('#datatable').dataTable({ "bStateSave": true, "fnStateSave": function(oSettings, oData) { localStorage.setItem('datatable&apos ...

Is there a more efficient method for implementing server side rendering in a Next.js application?

Currently, I am implementing server-side rendering in my Next.js application. This specific component is responsible for returning HTML content. I'm wondering if there are more efficient methods available? import Feature from 'components/home/Fea ...

The loop is being controlled but the data is not being added and shown in the HTML div

I am struggling to display JSON data in an HTML div using a for loop. While my control is entering the loop, the data is not being appended and displayed in the HTML div. Here is the JSON data: [{"id":"15","FirstName":"ranjan","MiddleName":"","LastName": ...

Methods for applying a style property to a div element with JavaScriptExecutor in Selenium utilizing C#

I have been attempting to change the style of a div element using JavascriptExecutor in C#, but so far, nothing has happened. IJavaScriptExecutor js = (IJavaScriptExecutor)driver; IWebElement element = driver.FindElement(By.XPath("//div[contains(@class, ...

Using the reduce method in JavaScript or TypeScript to manipulate arrays

Just exploring my culture. I have grasped the concept of the reduce principle var sumAll = function(...nums: number[]):void{ var sum = nums.reduce((a, b) => a + b , 0); document.write("sum: " + sum + "<br/>"); } sumAll(1,2,3,4,5); The r ...