簡體   English   中英

嘗試使用javascript打印頁面時出現錯誤請求

[英]bad request when trying to print a page using javascript

我正在嘗試使用javascript打印頁面,如果我單擊“打印”按鈕,則在彈出新窗口時出現“錯誤請求”。 這是我的JavaScript代碼,可通過捕獲當前位置並分配要打印的媒體CSS來打開一個窗口。

function printerFriendlyNew() {
    genPopUp = window.open("","printer","toolbar=no,location=no,scrollbars=yes,directories=no,status=yes,menubar=yes,resizable=yes,width=760,height=600");
    genPopUp.location.href = document.location.href + "&css=print";
    if (genPopUp.opener == null) genPopUp.opener = window;
    genPopUp.opener.name = "opener";
}

知道為什么我會收到“錯誤請求”錯誤嗎?

解決了這個問題……瘋了我,只是盲目附加“&css = print”,因為我有帶有查詢字符串的URL,它對那些字符串有效,而對那些沒有字符串的字符串無效。 我將其添加到現有的javascript中,從而解決了該問題。

var contain = document.location.href; 
if (contain.indexOf('?') == -1) 
  genPopUp.location.href = contain + "?css=print"; 
else 
  genPopUp.location.href = contain + "&css=print";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM