手动输入网址以移至下一页时,Chrome浏览器会加载脚本两次 [英] Chrome loading script twice when manually typing url to move to next page

查看:67
本文介绍了手动输入网址以移至下一页时,Chrome浏览器会加载脚本两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,我想这可能是Chrome错误或Visual Studio错误.我的脚本被加载了两次.为了验证该问题,我创建了一个测试脚本,该脚本设置了带有时间戳的cookie,以显示代码被追加了两次.如果我通过超链接导航到页面,则效果很好.同样,如果我按下来回按钮,它也可以正常工作-但是如果我在浏览器中手动键入第二页的网址,它将两次调用脚本.

I am having a bizarre issue that I'm thinking may be a Chrome bug or possibly a bug with Visual Studio. My script is getting loaded twice. To verify the issue I created a test script that sets a cookie with a timestamp to show the code is getting appended twice. If I navigate to the page via a hyperlink it works fine. Also if I hit the back and forth buttons it also works fine -- but if I manually type the url for the second page in the browser it calls the script twice.

这仅在Chrome中发生,并且仅在使用Asp.Net Web应用程序时发生. 如果我使用Asp.Net网站,则不会发生.这些是新的空Web应用程序,没有更改任何其他文件或设置.

This only happens in Chrome and only when using a Asp.Net Web Application. If I use a Asp.Net website, it does not occur. These are new empty web applications with no extra files or settings changed.

我的示例html页面如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Page 1</title>
    <script src="chromebug.js"></script>
</head>
<body>
    <h1>Page 1</h1>
    <a href="page1.html">Page 1</a>
    <a href="page2.html">Page 2</a>
    <a href="page3.html">Page 3</a>
</body>
</html>

而chromebug.js如下所示:

function getCookieTest(name) {
    var cookie, cookies = document.cookie.split(';');
    name = name + '=';
    while ((cookie = cookies.pop())) {
        cookie = cookie.replace(/^\s+/, '');
        if (cookie.indexOf(name) === 0) {
            return cookie.substring(name.length, cookie.length);
        }
    }

    return null;
}

function setCookieTest(name, value, secs) {
    var c;
    if (secs > 0) {
        c = new Date();
        c.setSeconds(c.getSeconds() + secs);
        c = name + '=' + value + '; expires=' + c.toGMTString() + '; path=/';
    } else {
        c = name + '=' + value + '; path=/';
    }
    document.cookie = c;
    return
}

console.log('chromebug loaded');
var getdata = getCookieTest('loaded') || '';
setCookieTest('loaded', getdata.toString() + document.title + ':' + new Date().getTime().toString() + '|', 10000);
getdata = getCookieTest('loaded');
console.log(getdata); // show what has been saved in the cookie

我错过了什么吗?有任何解决方法吗?

我什至制作了一个演示问题的视频: http://screencast.com/t/MpXfbDMBvfY

I even created a video where the issue is demonstrated: http://screencast.com/t/MpXfbDMBvfY

推荐答案

非常感谢stdob将我指向Live HTTP Headers扩展.如果您遇到类似的问题,建议您这样做. https://chrome.google.com/webstore/detail/live-http-headers/iaiioopjkcekapmldfgbebdclcnpgnlo?hl = zh-CN

Thanks so much to stdob for pointing me to the Live HTTP Headers extension. I'd recommend it if you are having a similar issue. https://chrome.google.com/webstore/detail/live-http-headers/iaiioopjkcekapmldfgbebdclcnpgnlo?hl=en

我的问题是Google Chrome预取优化.当您在地址栏中输入内容时,Google会预测您将要加载的页面并开始下拉脚本.如果您的脚本在加载时执行了一些加载或cookie操作,则这是有问题的.取消选中此选项时,您应该会看到重复的负载消失了.

The issue in my case is with the Google Chrome prefetch optimization. When you type in the address bar, Google anticipates the page you will be loading and starts pulling down the scripts. If your script preforms some loading or cookie action upon loading this is problematic. When you uncheck this option you should see the duplicate loads go away.

但是,这是在不更改设置的情况下进行控制的简便方法.这一点特别重要,因为您无法控制最终用户的设置.

However this is an easy way to control this without changing your settings. This is especially important since you have no control over your end-user's settings.

if (document.webkitVisibilityState && document.webkitVisibilityState == 'prerender') {
// avoid performing load logic
}

这篇关于手动输入网址以移至下一页时,Chrome浏览器会加载脚本两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆