1

I am trying to print a page with window.print but it ain't working in all the browsers.

This is the code that i am using:

<div class="user_buttons">
    <!--Test 1-->
    <A HREF="javascript:window.print()"><IMG SRC="images/print.png" BORDER="0"</A>

    <!--Test 2--> 
    <FORM>
        <INPUT TYPE="button" onClick="window.print()">
    </FORM>

    <!--Test 3-->
    <SCRIPT LANGUAGE="JavaScript"> 
    if (window.print) {
        document.write('<form><input type=button name=print value="Print" onClick="window.print()"></form>');
    }
    </script>

    <!--Test 4-->
    <img src="images/print.png" onclick="window.print()">
    <div><a href="overzicht.php"><img src="images/overzicht.png" title="Terug naar overzicht"></a></div> 
</div>

As you can see i am trying multiple solutions given by the internet. What is frustrating is that those codes are working on the demo sites but they aren't on my page. I post my code in JSF. The JSF example will not be a working example but it will have the entire code in the javascript area. The link for the entire code is here: http://jsfiddle.net/7bRNu/

4

2 回答 2

7

I finally got it. It was a very painfull process of searching errors in a huge mess of code. The next time you ask a question on stackoverflow, please make sure that you broke the problem down into smaller pieces and post only the code that you think is probably the cause of your problem.

In the very bottom of your entire code, there is a little script-section in which it says:

var print = document.getElementById("print").value;

You are in the global scope, meaning that every variable you declare will be a property of window. Therefore, by writing print = you actually redefine window.print. Change the name of this variable and you should be good. The following line is only an example. You can choose whatever variable name you like. Just don't use print.

var printValue = document.getElementById("print").value;
于 2013-10-18T12:51:55.887 回答
3

Here is one method isolated:

http://jsfiddle.net/5PumN/

<A HREF="javascript:window.print()"><IMG SRC="images/print.png" BORDER="0"</A>

It works just fine here.

于 2013-10-18T12:36:34.590 回答