2010-07-21 64 views
7

我有一個頁面調用window.print();在頁面底部。我無法訪問window.print();它由服務器生成,我無法觸摸它。基本上因爲IE瀏覽器,我需要在打印對話框出現之前,但在頁面加載之後執行一些javascript。我不能這樣做,因爲只要它到達window.print();打印對話框出現。我仍然需要打印,但首先我需要運行myFunction()然後我可以window.print();執行函數在頁面到達之前window.print()

<html><head></head><body></body><!--no access from here--><script>window.print();</script></html> 

回答

16

您應該能夠覆蓋它是這樣的...

var _print = window.print; 
window.print = function() { 
    alert("Hi!"); 
    // do stuff 
    _print(); 
} 
+0

我可以發誓我試過了。儘管如此,這種方式很好謝謝! – joe 2010-07-21 21:22:17

+0

Awww,打我一拳! +1。 – 2010-07-21 21:23:06

+0

@joe沒問題,歡迎來到Stack Overflow!要「接受」這個答案,您可以在投票向下箭頭下單擊複選標記。 – 2010-07-21 21:23:51

8

「基本上是因爲IE的我需要......」

如果你只需要IE瀏覽器支持,請參閱onbeforeprint事件。

window.onbeforeprint = function() { 
    // some code  
} 

這裏的好處是,代碼將無法在不支持onbeforeprint,這是相當多的,除了IE瀏覽器的每一個瀏覽器中運行。

+0

哇那新一!! – joe 2010-07-21 21:30:09

0

嘗試把另一<script>標籤中的一個,你沒有獲得如前:

<html><head></head><body></body><script>alert('Hello There !');</script><script>window.print();</script></html>