简体   繁体   中英

Dns prefetching with javascript

I'm changing the dns-prefetching href with javascript. I want to know if it is effective. I'have 2 website that share the same javascript.

So i do something like this:

<link id="prefetching" href="//www.site1.com" rel="dns-prefetch" />




var currentDomain = document.domain;

if (currentDomain.contains("site1")) {
    $("prefetching").href = currentDomain.replace("site1", "site2");
    return;
}
if (currentDomain.contains("site2")) {
    $("prefetching").href = currentDomain.replace("site2", "site1");
    return;
}

It wouldn't hurt to dns-prefetch both domains, simply by adding two <link rel="dns-prefetch"> tags.

Waiting for the javascript to be run, before being able to interpret the (changed) dns-prefetch tags in the <head> of your document would undo the kind of improvement you're after.

Nonetheless, if you do want to find out if your method is working, in Chrome you can see the DNS-prefetching info by opening chrome://dns/ in the address bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM