从MVC应用程序中的其他站点获取带有src的iframe内容

时间:2022-12-07 21:16:38

I want to have a way somehow to get the iframe content when some action is triggered in the iframe window. All the content in the iframe comes from other website. There's a textbox and a button in the iframe and I want to detect when the content will be updated and if there's specific content in the iframe then I need to perform some actions. Currently what I have is this:

我希望在iframe窗口中触发某些操作时以某种方式获取iframe内容。 iframe中的所有内容均来自其他网站。 iframe中有一个文本框和一个按钮,我想检测内容何时更新,如果iframe中有特定内容,那么我需要执行一些操作。目前我拥有的是:

<script type="text/javascript">
    $(document).ready(function() {
        var timesRefreshed = 0;
        $('#frame').load(function () {
            if (timesRefreshed == 1) {
                var data = {
                    transactionID : '@Model.OrderId'
                };
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("ConfirmationStep", "Cart")',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: function(result) {                       
                    }
            });
            }
            timesRefreshed++;
            if (timesRefreshed == 2) {
                window.location.href = '/Home/PaymentComplete/';
            }           
        });
    });  
</script>

Using this approach doesn't give me a lot of options but simply checks how many times the iframe has been loaded, but for example if the user enters incorrect data the iframe might be loaded but with message saying that the value is not correct.

使用这种方法并没有给我很多选项,只是简单地检查iframe加载了多少次,但是例如,如果用户输入了错误的数据,iframe可能会被加载,但会显示消息说该值不正确。

Now, this piece of code will see that the timesRefreshed equals 2 and it will redirect to the other view. I want to check the actual content of the iframe. I checked the innerHTML property but its empty most probably because the iframe is hosting content from other website.

现在,这段代码将看到timesRefreshed等于2,它将重定向到另一个视图。我想检查iframe的实际内容。我检查了innerHTML属性,但它很可能是空的,因为iframe正在托管来自其他网站的内容。

1 个解决方案

#1


5  

Try jQuery postMessage. Demo

试试jQuery postMessage。演示

jQuery postMessage enables simple and easy window.postMessage communication in browsers that support it (FF3, Safari 4, IE8), while falling back to a document.location.hash communication method for all other browsers (IE6, IE7, Opera).

jQuery postMessage在支持它的浏览器(FF3,Safari 4,IE8)中实现简单易用的window.postMessage通信,同时回退到所有其他浏览器(IE6,IE7,Opera)的document.location.hash通信方法。

With the addition of the window.postMessage method, JavaScript finally has a fantastic means for cross-domain frame communication. Unfortunately, this method isn’t supported in all browsers.

通过添加window.postMessage方法,JavaScript终于拥有了跨域框架通信的绝佳方法。不幸的是,并非所有浏览器都支持此方法。

#1


5  

Try jQuery postMessage. Demo

试试jQuery postMessage。演示

jQuery postMessage enables simple and easy window.postMessage communication in browsers that support it (FF3, Safari 4, IE8), while falling back to a document.location.hash communication method for all other browsers (IE6, IE7, Opera).

jQuery postMessage在支持它的浏览器(FF3,Safari 4,IE8)中实现简单易用的window.postMessage通信,同时回退到所有其他浏览器(IE6,IE7,Opera)的document.location.hash通信方法。

With the addition of the window.postMessage method, JavaScript finally has a fantastic means for cross-domain frame communication. Unfortunately, this method isn’t supported in all browsers.

通过添加window.postMessage方法,JavaScript终于拥有了跨域框架通信的绝佳方法。不幸的是,并非所有浏览器都支持此方法。