gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:55:52 26 4
gpt4 key购买 nike

我正在尝试一周的时间来找到解决以下问题的方法。我有一个 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/

但是当我使用 HttpRequest(url);out of new Fingerprint2().get(function(result) { 时,它工作得很好。但最大的问题是 url var。(因为 ir 无法在功能之外访问)

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

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

最佳答案

问题就在这里:

document.write(e.responseText)

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

例如:

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

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

<小时/>

更多解释:

当 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 http://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
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;
}

关于javascript - 当我在函数中使用新的 XMLHttpRequest 时,所有页面都会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com