隐藏Iframe刷新Ajax选项

Hidden Iframe Refresh Ajax Alternative?

本文关键字:选项 Ajax 刷新 Iframe 隐藏      更新时间:2023-09-26

我使用隐藏的Iframe在后台刷新页面,以保持访问令牌新鲜,而无需刷新整个页面。这在所有浏览器中都很有效,除了IE,在IE中,当刷新发生时,IE会从桌面上的任何东西上窃取焦点。所以我的问题是有一个ajax替代隐藏Iframe或更好的方法来处理这个?这是我的代码:

<iframe Id="hidden" src="https://www.xxxxxxxx.com" 
    style="visibility: hidden;
        position: absolute;
        left: 0; top: 0;
        height:100%; width:0;
        border: none;" 
    onload="refresh()">
</iframe>
<script>
    function refresh() {
        console.clear();
        setTimeout(refreshCookie, 60000);
        function refreshCookie() {
            document.getElementById('hidden').src = document.getElementById('hidden').src;
        };
    };
</script>
const refresh = () => {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = () => {
        if (this.readyState == 4 && this.status == 200) {
              // Request ok.
        }
    };
    xhttp.open("GET", "YOUR.URL", true);
    xhttp.send();
}    
setInterval(refresh, 10000); //refresh every 10 seconds.

一个基本的AJAX请求就可以了

编辑:——正如Archer在你的问题的评论中所说,如果它是跨域的,那么它将不起作用。