简体   繁体   English

jQuery-从iframe禁用/删除JavaScript

[英]jQuery - disable / removing javascript from iframe

I have an iframe inside my page with the following code: 我的页面内有一个iframe,其中包含以下代码:

...
<script  language="javascript"  type="text/javascript">

    function clearMessage() {
       window.print();
    }

    $(function () {
        var state=0;
        if (state==1) {
            window.print();
            window.opener.location = window.opener.location.href;
        }

        var sentToPrint=0;
        if (sentToPrint==1)
        {
            window.print();
            window.opener.location = window.opener.location.href;
        }

    });

    window.onunload = refreshParent;
    function refreshParent() {
        if (('1') == '1') {
            window.opener.location = window.opener.location.href;
            //window.opener.location = 'mainPage.aspx'
        }
    }
</script>
...

Basically what it does is printing the page on form submit or if I having "print=1" in the URL, which in my case I sending "print=0" because I don't want the iframe to be printed. 基本上,它的工作是在表单提交上打印页面,或者如果URL中包含“ print = 1”,那么我发送“ print = 0”是因为我不想打印iframe。

so I want to disable this part or removing this part form my parent page using jQuery. 所以我想禁用此部分或使用jQuery从我的父页面中删除此部分。

Is there any way to disable the printing function or removing it from the iframe? 有什么方法可以禁用打印功能或将其从iframe中删除吗?

thank you! 谢谢!

If you don't want iframe part to be printed then use print media query @media print to hide that iframe while printing 如果您不想打印iframe部件,请在打印时使用打印介质查询@media print隐藏该iframe

@media print {
   iframe {
      display: none;
   }
}

Finally, I found how to submit the form without printing the page, I just copied the inputs from the iframe and submitted the form using ajax 最后,我发现了如何在不打印页面的情况下提交表单,我只是复制了iframe的输入,然后使用ajax提交了表单

$('#myFrame').one('load', function(){
    var eventTriger     = $("#myFrame").contents().find("#__EVENTTARGET").val();
    var eventArgu       = $("#myFrame").contents().find("#__EVENTARGUMENT").val();
    var viewState       = $("#myFrame").contents().find("#__VIEWSTATE").val();

    var json = {
        '__EVENTTARGET'     : eventTriger,
        '__EVENTARGUMENT'   : eventArgu,
        '__VIEWSTATE'       : viewState
    };

    var request = $.ajax({
      url: "url",
      type: "POST",
      data: json,
      dataType: "html"
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM