首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在一个新的window.open上启动jquery,并避免“未捕获ReferenceError:$ is not defined”错误?

如何在一个新的window.open上启动jquery,并避免“未捕获ReferenceError:$ is not defined”错误?
EN

Stack Overflow用户
提问于 2018-08-30 00:49:00
回答 1查看 811关注 0票数 0

我有一个带有对象应用程序“type=/pdf”和两个select标记的div,它们在更改时更改对象的src。此外,还有一个图标可以打开一个包含以下内容的新窗口(object + html select tag):

代码语言:javascript
运行
复制
<div id="navegador_pdfs">

    <div id="btn_toggle_pdf_navigator">
        <a href="#" onclick="destacar();"><?php echo $this->Bootstrap->glyphicon('new-window'); ?></a>
    </div>

    <div style="position: absolute; bottom: 0; background-color: rgba(0, 0, 0, 0.75); color: #FFF; text-align: center;">
    Pasta Digital
    <?php
    echo $this->Form->select('linhas', 
                    [ 'Documentos: ' => $documentos, ' Ou: ' => [ '' => 'Ver Todos' ] ],
                    ['default' => $documento_id,
                    'class' => 'form-control truncated', 
                    'onchange' => " if( $.isNumeric($(this).val()) ) { "
                        . "$('#myPdf').attr('data', '" . $this->Url->build([ "controller" => "binario", "action" => "visualizar"]) . "/'+$(this).val()); "
                        . "} else { "
                        . "$('#myPdf').attr('data', '" . $this->Url->build([ "controller" => "binario", "action" => "ver-todos", "?" => [ 'numero_processo' => $numero_processo ]]) . "'); "
                        . "} "]);
    ?>
    </div>
    <div style="position: absolute; bottom: 0; right: 15px; background-color: rgba(0, 0, 0, 0.75); color: #FFF; text-align: center;">
    Pasta Digital Externa
    <?php
    echo $this->Form->select('linhas', 
                    [ 'Documentos: ' => $documentosExternos, ' Ou: ' => [ '' => 'Ver Todos' ]  ],
                    ['class' => 'form-control truncated', 
                    'onchange' =>  " if( $(this).val() != '' ) { "
                        . "$('#myPdf').attr('data', '" . $this->Url->build([ "controller" => "solicitacao", "action" => "downloadDocumentoTj"]) . "/'+$(this).val()); "
                        . "} else { "
                        . "$('#myPdf').attr('data', '" . $this->Url->build([ "controller" => "solicitacao", "action" => "baixarTodosDocumentos", $cnj ]) . "'); "
                        . "} "
                        ]);
    ?>
    </div>
    <object id="myPdf" type="application/pdf" data="<?php echo ($documento_id > 0 ) ? $this->Url->build([ "controller" => "binario", "action" => "visualizar", $documento_id]) : "" ; ?>" width="100%" height="100%"></object>
</div>

以及在新窗口中打开此内容的脚本:

代码语言:javascript
运行
复制
function destacar(){

    const leftpos = screen.width / 2;
    const toppos = 0;

    var params  = 'width='+ (screen.width/2);
    params += ', height='+ (screen.height/1.2);
    params += ', top='+toppos+', left='+leftpos;
    params += ', fullscreen=yes';

    var divText = '<html><head>';
    divText += '<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">';
    divText += '<link rel="stylesheet" type="text/css" href="/css/theme.css">';
    divText += '</head><body style="padding: 0px 5px;">';

    var myWindow = window.open('','',params);
    var doc = myWindow.document;
    doc.open();
    doc.write(divText);

    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/js/jquery.min.js';

    $(doc.head).append(script);

    doc.write(document.getElementById("navegador_pdfs").outerHTML);

    doc.close();


    $('#form_novo_despacho').removeClass('col-md-6');
    $('#form_novo_despacho').addClass('col-md-12');
    $('#navegador_pdfs').hide();

    myWindow.onunload = function(){ 
        $('#form_novo_despacho').removeClass('col-md-12');
        $('#form_novo_despacho').addClass('col-md-6');
        $('#navegador_pdfs').show();
    };

}

然后,当我在打开的窗口上更改任何select标记的值时,可能会在控制台上看到错误:

代码语言:javascript
运行
复制
Uncaught ReferenceError: $ is not defined
at HTMLSelectElement.onchange (VM1774 nova:1)

我认为在最近打开的这个新窗口上没有初始化jQuery。有没有一种方法可以初始化jQuery,或者只是我遗漏了另一件事?

谢谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52082481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档