Question: I'm trying to show a preview of the print-styled page before the print dialog, by applying the print stylesheet to the screen stylesheet.

This doesn't work:

```
$('link[media*="screen"]').attr("href",url+"css/print/default.css");
window.print();

```

But with an alert() before the print(), it DOES work:

```
 $('link[media*="screen"]').attr("href",url+"css/print/default.css");
 alert("Ok?");
 window.print();

```

Any hints?

Answer: 
This function delays execution of `window.print` a few seconds (`delay` parameter, default about 5 seconds):

```
function doPrint(delay){
 delay = delay || 5; //default to 5 seconds
 $('link[media*="screen"]').attr("href",url+"css/print/default.css");
 setTimeout(function(){window.print();},delay*1000);
}

```

You could also restore the original css after the print dialog by putting the reverse of `$('link[media*="screen"]').attr("href",url+"css/print/default.css");` into the function called from `setTimeout`.
It takes time to load a stylesheet. Without the alert, you are calling `print` before enough time has passed for the stylesheet to be loaded.

> 
> Any hints?
> 
> 
> 

Browsers have built in print and print preview functions. Avoid wheel reinvention.