Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据文档位置从Js文件重定向_Javascript_Html - Fatal编程技术网

Javascript 根据文档位置从Js文件重定向

Javascript 根据文档位置从Js文件重定向,javascript,html,Javascript,Html,我正在使用下面的代码来重定向我的js文件。。但我希望我的重定向基于顶部位置。例如,如果有人访问xyz.com,它会重定向到mydomain.com,那么我需要添加什么代码?我想它可能像indexOf'xyz.com' loadScript("http://j.maxmind.com/app/geoip.js", function() { var country = geoip_country_code(); if (country === "US") { win

我正在使用下面的代码来重定向我的js文件。。但我希望我的重定向基于顶部位置。例如,如果有人访问xyz.com,它会重定向到mydomain.com,那么我需要添加什么代码?我想它可能像indexOf'xyz.com'

loadScript("http://j.maxmind.com/app/geoip.js", function() {
    var country = geoip_country_code();

    if (country === "US") {
        window.location = "http://mydomain.com/";
    }
});

function loadScript(url, callback) {
    // adding the script tag to the head as suggested before
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url;

   // then bind the event to the callback function 
   // there are several events for cross browser compatibility
   script.onreadystatechange = callback;
   script.onload = callback;

   // fire the loading
   head.appendChild(script);
}

我不能完全肯定我理解你的问题

始终引用当前显示的网页的对象。如您所知,您可以更改window.location并在浏览器中更改页面

window.location还有几个属性可以帮助分析其内容,有关详细信息,请参阅引用的链接。我想window.location.host就是您需要的

因此,如果我正确理解你的问题,你可以替换

var country = geoip_country_code();

if (country === "US") {
    window.location = "http://mydomain.com/";
}


我认为至少java和php标记不适合这个问题。另外,可能是html标记的document.referer!=youtube.com{var refereringurl=document.referer;var local=Math.floorMath.random*100000000;location.href=http://domain.com? +local;}我正在使用此代码,但我希望此函数在基于cookie的每个会话中只执行一次。您知道怎么做吗?
if (window.location.host === 'xyz.com') {
    window.location = "http://mydomain.com/";
}