[SalesForce] Printing Visualforce page causes crash in IE

We have a simple outputLink whose only function is to print the current page. This is working fine in Firefox and Chrome, but in IE8 it opens the Print dialog box and then promptly crashes.

<apex:outputLink value="#" styleClass="navImageCustom printImage" title="Print" onclick="window.print();"/>

In IE9 it also opens the Print dialog, but the page refreshes and gives the error "The name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores.”"

I tried adding a "return false;" to the onclick but the crash remains. What's the easiest way to print a Visualforce page?

Edit: <div class="navImageCustom printImage" onclick="window.print(); return false;"></div>
also fails.

Best Answer

The issue is that it is trying to go to a URL of window.print(); return false;, e.g., https://c.na14.visual.force.com/apex/window.print(); return false. That page doesn't exist so it is trying to give you the intermediate page where you can choose to create a VF page with that name, but the name isn't a valid page name so you get that error.

You can see the same error if you type in any invalid page name. For example, try using something with a space: https://c.na14.visual.force.com/apex/bad name.

If you want to be able to use the same basic code, I think you can just use the javascript pseudo-protocol to get around it.

Change your onclick from

onclick="window.print(); return false"

To

onclick="javascript:window.print(); return false"

Alternatively, you could use something like jQuery to attach an event handler and then call window.print() from the event handling JavaScript code.

Related Topic