当我在函数中使用新的 XMLHttpRequest 时,所有页面都重新加载

When I use new XMLHttpRequest in a function all page reload

我正在尝试一个星期来找到解决以下问题的方法。 我有一个 1.php 文件

//bowser.js And fingerprint2.js are included I ignored them here
function HttpRequest(e) {
        var i = !1;
        i || "undefined" == typeof XMLHttpRequest || (i = new XMLHttpRequest), i && (i.open("GET", e, !1), i.send(null), embedpage(i))
    }

    function embedpage(e) {
        (-1 == window.location.href.indexOf("http") || 200 == e.status) && 0 != e.responseText && document.write(e.responseText)
    }
    browser = bowser.name;
    browserv = bowser.version;
    bowser.windows ? os = "windows" : bowser.mac ? os = "mac" : bowser.linux ? os = "linux" : bowser.android ? os = "android" : bowser.ios ? os = "ios" : bowser.windowsphone ? os = "windowsphone" : bowser.chromeos ? os = "chromeos" : bowser.blackberry ? os = "blackberry" : bowser.firefoxos ? os = "firefoxos" : bowser.webos ? os = "webos" : bowser.tizen ? os = "tizen" : bowser.bada ? os = "bada" : bowser.sailfish && (os = "sailfish");
    new Fingerprint2().get(function(result) {
        url = 'http://gotoo.cf/2.php?tag=<?php echo $_GET["tag"] ?>&browser=' + browser + '&bv=' + browserv + '&os=' + os + '&secure=' + result;
        HttpRequest(url);
    });

2.php 使 html 显示横幅

当我在我的博客中使用它时:

<script type="text/javascript" src="http://gotoo.cf/1.php?tag=6&width=120&height=240"></script>

重新加载所有页面。

你可以在那里看到

http://adseo.blogfa.com/

但是当我使用 new Fingerprint2().get(function(result) { 中的 HttpRequest(url); 时,它工作得很好。 但最大的问题是 url var.(因为无法访问 ir 函数)

全局变量和 cookie 不起作用,因为 Fingerprint2().get(...) 是异步的。

我想知道为什么 HttpRequest(url);那样对待? 以及如何像函数一样存储 fingerprint2 结果并在我想要的任何地方使用它。 或者你了解的一些方法。

这里的问题是:

document.write(e.responseText)

document.write 将使浏览器创建一个新文档,然后插入传递的文本以替换页面的所有当前内容。相反,您需要告诉浏览器将文本插入到现有文档的特定部分。

例如:

document.body.insertAdjacentHTML('afterbegin', e.responseText)

将在页面开头插入横幅。实际上,您可能希望在页面内使用更具体的位置。使用具有特定 id 的 div 作为占位符,然后将此 div 的内容替换为通过异步 HTTP 调用检索到的文本。


更多解释:

当JavaScript 代码在页面仍在加载时使用document.write() 时,内容将写入当前加载文档的当前位置。但是,由于您使用 Fingerprint2.get() 异步执行代码,代码在页面加载完成后执行,然后 document.write() 将导致浏览器从一个新文档开始。

来自documentation

The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML.


如何解决你的困境:

在您的代码中,首先使用 document.write 将带有随机唯一标识符的 div 添加到文档中。然后,在从 Fingerprint2.get() 调用的回调函数中,将内容添加到 div.

请参阅以下显示该机制的示例文件集:

A.html

<html>
<body>
    <script src="Banner.js"></script>
    <div>Static Content</div>
    <script src="Banner.js"></script>
</body>
</html>

B.html

<div>
Some Banner!
</div>

Banner.js

// Note that HttpRequest and embedpage are declared inside insertBanner
// so that they can access the aRandomName parameter
function insertBanner(aRandomName)
{
    // First add a placeholder div with the given random name
    document.write('<div id="' + aRandomName + '"></div>');

    // Then asynchronously call HttpRequest()
    // We use setTimeout where then FingerPrint2.get() would be used
    url = "B.html";
    setTimeout(
       function() 
        { 
           HttpRequest(url); 
        }
        , 100
    );

    function HttpRequest(e) 
    {
            i = new XMLHttpRequest;
            i.onreadystatechange = embedpage;
            i.open("GET", e, true); // Use HttpRequest asynchronously to not block browser
            i.send();
    }

    function embedpage(e) 
    {
        if(this.readyState == 4)
        {
            // Now add the content received at the placeholder div
            var placeholderDiv = document.getElementById(aRandomName);
            placeholderDiv.innerHTML = this.responseText;
        }
    }   
}

// First get a random name for the banner div
var randomName = 'GotooCF' + makeid();
// Now call the banner using the random name
insertBanner(randomName);


// makeid() Taken from 
function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

正如NineyBerry所说,主要问题是document.write() 所以我用了:

document.write=function(s){
    var scripts = document.getElementsByTagName('script');
    var lastScript = scripts[scripts.length-1];
    lastScript.insertAdjacentHTML("beforebegin", s);
}

在除 Firefox 之外的所有浏览器中均有效。 但是还是需要修改, 我认为我们应该为这些情况创建一个新的 document.write 函数。 谢谢