简体   繁体   中英

jQuery - disable / removing javascript from iframe

I have an iframe inside my page with the following code:

...
<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.

so I want to disable this part or removing this part form my parent page using jQuery.

Is there any way to disable the printing function or removing it from the 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

@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

$('#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"
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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