2010-04-09 35 views
0

我一直在爲此付出一段時間,並決定是時候尋求幫助。使用javascript寫入新窗口...獲取訪問被拒絕

我想創建一個aspx頁面上的打印功能,我使用JavaScript來做到這一點:

 function PrintContentCell() { 
     var display_setting = "toolbar=yes, location=no, directories=yes, menubar=yes,"; 
     display_setting += "scrollbars=yes, width=750, height=600, left=100, top=25"; 

     var content_innerhtml = document.getElementById("testTable").innerHTML; 

     var document_print = window.open("Loading.htm", "", display_setting); 
     document_print.document.open(); 
     document_print.document.write('<html><head><title>Print</title></head>'); 
     document_print.document.write('<body style="font-family:verdana; font-size:12px;" onLoad="self.print();self.close();" >'); 
     document_print.document.write(content_innerhtml); 
     document_print.document.write('</body></html>'); 
     document_print.print(); 
     document_print.document.close(); 
     return false; 
    } 

我得到「拒絕訪問」,當腳本嘗試寫入新的窗口。 Loading.htm文件只是一個非常苗條的HTML文檔,寫出文本「正在加載...」。我本來希望看到這個線程後,這將工作:IE 6/7 Access Denied trying to access a popup window.document

任何人都可以幫忙嗎?

+0

它看起來像你試圖打開/關閉在彈出窗口通過onload *和*從開瓶器窗口。這是故意的嗎? – scunliffe 2010-04-09 10:52:54

+0

你好。你是對的,它應該足以讓他們在onLoad事件中。我剛從這個頁面複製了這個測試的大部分腳本:http://nice-tutorials.blogspot.com/2009/05/print-using-javascript.html – Sohape 2010-04-12 06:50:22

回答

0

難道你不能簡單.write()創建一個空的(window.open("", ...))彈出後的「加載」標記嗎?

它會避免到服務器之旅,似乎對用戶更加敏感並解決您的問題。事實上,因爲你只是在客戶端洗牌數據,所以渲染HTML的時間真的需要加載橫幅嗎?

+0

我一開始試過,但那給了我寫入()時出現「訪問被拒絕」錯誤。因此,我嘗試使用靜態HTML加載頁面,看看是否有幫助 - 但不幸的是它沒有:( – Sohape 2010-04-12 06:55:45

3

如果你想有一個新的,空的彈出窗口,以寫入,通常的做法是用一個空字符串的URL:

window.open('', '_blank', features) 

沒有意義,試圖從當你在服務器上打開HTML在它甚至有機會加載之前立即替換所有內容。 (您的空窗口名稱""也可能會導致問題。)

但是,無論如何這不是實現頁面的「打印版本」的好方法。相反,使用print stylesheet,它隱藏除testTable的所有內容之外的所有內容。

+0

嗨bobince。非常感謝您的答案。我有Loading.htm的原因是因爲我得到了Access拒絕以及沒有它,並引用來自此線程的StackOverflow用戶David http://stackoverflow.com/questions/735136/ie-6-7-access-denied-trying-to-access-a-popup-window-文件:「IE認爲」關於:空白「是一個不安全的URL,它不會讓你說話,我會創建一個」正在加載...「靜態HTML文件,並打開它。」 – Sohape 2010-04-12 06:52:32

+0

我仍然得到訪問被拒絕的錯誤,也當我提供沒有加載HTML和'_blank'目標:( 我會嘗試閱讀你的建議,使用css打印,並看看我是否適用 – Sohape 2010-04-12 06:53:59

+0

適用於我'。'''是與''about:blank''不同的是,你不是在父窗口ar上設置'document.domain'你呢? – bobince 2010-04-12 08:47:38