脚本在printWindow命令中不起作用

Script not working in printWindow command

本文关键字:不起作用 命令 printWindow 脚本      更新时间:2023-09-26

我正在创建一个弹出窗口,其中有一个按钮在单击时不起作用。下面是我的javascript:

      function showPic(pic_id, user_pic, name, caption, date) {
        span = getTimeSpan(date);
            printWindow = window.open('', '', 'width=650, height=710, scrollbars');
        printWindow.document.open();
        var htmlcode = "<img src='" + pic_id + "'> </br><table border=0 cellpadding=3 cellspacing=1><tr><td align=left valign=top><img src='" + user_pic + "' width=50></td><td align=left valign=top>" + span + "&nbsp; - <b>" + name + ":</b> &nbsp;" + caption;
        htmlcode += "</br><input type='"button'" value='"See all photos'" onclick='"showUserPhotos('" + name + "')'" /></td></tr></table>";
        htmlcode += "<script>function showUserPhotos(user_name) { alert('"username='" + user_name)}";
        printWindow.document.write(htmlcode);
        printWindow.document.close();
    }

你会注意到,我在上面的逻辑中没有"script"结束标记,因为如果我使用它,程序会给我一个错误。点击弹出窗口中的按钮时,showUserPhotos不会执行。有没有办法在弹出窗口中执行脚本?

首先创建您的窗口。换句话说,使用window.open('urlofexistingpage.html')。打开一个已经有JavaScript的页面。如果这对你不起作用,并且你想动态创建窗口脚本,那么你需要使用jQuery.ajax()XMLHttpRequest对象。