Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding media queries from js

Tags:

javascript

css

I have a page where I open a new window like so

var myWindow = window.open("", "", "width=755.9100,height=347.7200");
myWindow.document.body.append(img);

I want to add styles some style to this new window from the previous window

@page {size: A4;margin: 0;@media print {html, body {width: 210mm;height: 297mm;}

How do I add this to head tag in html through js?

like image 877
Usama Rehan Avatar asked Mar 10 '23 17:03

Usama Rehan


2 Answers

There is the window.matchMedia function in js:

The usage is quite simple:

if (window.matchMedia("(min-width: 400px)").matches) {
    /* the viewport is at least 400 pixels wide */
} else {
    /* the viewport is less than 400 pixels wide */
}

One more useful article is here.

like image 131
Sergey Avatar answered Mar 19 '23 14:03

Sergey


style is also an HTML element, so you can append that in a similar manner as you are appending the img element

var myWindow = window.open("", "", "width=755.9100,height=347.7200");
myWindow.document.body.append(img);
var style = myWindow.document.createElement("style")
style.innerHTML = "@page {size: A4;margin: 0;@media print {html, body {width: 210mm;height: 297mm;}"
myWindow.document.head.append(style);
like image 32
kloik friend Avatar answered Mar 19 '23 14:03

kloik friend