0

Hi I have been writing a website for the last couple of weeks but cannot get the Javascript function window.print() to run locally in my browser. I do not have server space yet so I cannot check the online functionality. Here is my code:

    <form>
        <input type="button" value="Print this page" onclick="window.print();" />
    </form> 

Does anyone know if this function can run on a local machine? Or is there a problem with my code? Thanks

Huangism
  • 16,278
  • 7
  • 48
  • 74
Celt
  • 2,469
  • 2
  • 26
  • 43
  • 3
    it works locally for me – Kevin B Jul 30 '14 at 14:25
  • 1
    Works just fine for me. [Here's a fiddle with your code](http://jsfiddle.net/7UQMJ/). What exactly is going wrong? – Mike Cluck Jul 30 '14 at 14:26
  • 2
    This is probably a browser issue rather than an issue with your code – imbondbaby Jul 30 '14 at 14:28
  • Nothing happens when I click the button, I have tried in firefox and chrome and nothing happens in either of them. Firebug does not detect an error too so I dont knwo what is wrong. Is there a script I have to include for it to work? – Celt Jul 30 '14 at 14:40
  • Just tried it there on a new page with no information and it worked, must be a problem with the page I have developed. – Celt Jul 30 '14 at 14:45
  • there must be a javascript error somewhere else on the page thats the only thing that would prevent javascript like that working however the fact that it worked after an `alert` is a bit strange as that would say there is no error. is there any other javascript in the page before it? including analytics codes – Barkermn01 Jul 30 '14 at 22:41
  • no the rest of my JavaScript, including analytics, is in the bottomof the body. It's very strange when I take out all the content and just leave the button it works fine, but when I have the content in it won't work, very strange – Celt Jul 31 '14 at 16:19

3 Answers3

1

The syntax is fine. It should work.

You might also want to try onclick="javascript:window.print();" and see if that works.

Another option you can try then is to create a function inside a script tag and call it from your onclick event:

<input type="button" value="Print this page" onclick="PrintMe()" />

<script>
    function PrintMe(){
        window.print();
    }
</script>
MikeV
  • 585
  • 3
  • 11
  • I thought that was used in anchor tags. Can it be used in regular event handlers as well? – Edward Jul 30 '14 at 14:38
  • I have tried that in an anchor tag and tried the normal call in buttons to no avail. – Celt Jul 30 '14 at 14:41
  • 2
    Are you able to run any javascript code? You might want to run an alert("test"); inside a document.ready to see if for some reason you have javascript turned off on your browser – MikeV Jul 30 '14 at 14:44
  • Yes if I put an alert function before or after the print() function it runs, it's very strange. Is there someway I can post the whole page code? I dont want to post it in the question – Celt Jul 30 '14 at 14:57
  • Oh, and by the way... this may seem like a silly question but do you have any printers defined on your computer? – MikeV Jul 30 '14 at 15:32
  • I tried putting it in a seperate function and yeah I have printers defined on the computer, thanks though! Is there somewhere I can post the full page code? It's around 200 lines – Celt Jul 31 '14 at 08:18
1

It might be a problem with the form tag..

try using button without form requirement

<button onclick="window.print();">Print this page</button>

Or if you are using the form

<form>
    <input type="button" value="Print this page" onclick="window.print();return false;" />
</form> 

if alert works and then enables this to work what happens if you try

onclick="console.log(window.print());return false;"
Barkermn01
  • 6,781
  • 33
  • 83
  • tried it with buttons and anchor tags, nothing works, thanks though. – Celt Jul 30 '14 at 14:42
  • if your using IE 9 or newer. press F12, if your in FF or Chrome right click inspect Element and then for them all click the console tab. – Barkermn01 Aug 01 '14 at 16:32
0

Capitilize the C in onClick. That might help. That's how I learned it, anyway.

Edward
  • 79
  • 8
  • I learned it that way too but apparently it doesnt make a difference – Celt Jul 30 '14 at 14:42
  • Some other relevant information on window.print() here. http://stackoverflow.com/questions/9268840/determine-whether-browser-supports-printing – Edward Jul 30 '14 at 14:50