2012-05-26 3 views
0

리디렉션을 위해 내 js 파일에 대해 아래 코드를 사용하고 있습니다. 그러나 최상위 위치를 기반으로 리디렉션을 원합니다. 예를 들어 누군가 xyz.com을 방문하여 mydomain.com으로 리디렉션하면 어떤 코드를 추가해야합니까? 나는 문서 위치에 따라 Js 파일에서 리디렉션

window.location

는 항상 현재 표시된 웹 페이지를 참조하는 객체이다

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); 
} 
+1

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

를 대체 할 수있는 나는 적어도 자바와 PHP 태그는이 질문에 대한 부적절한 생각합니다. 또한, 아마도 html 태그 – jpm

답변

0

내가 당신의 질문을 이해 완전히 확실하지, 같이 IndexOf ('xyz.com')처럼 될 수 있다고 생각 . 이미 알고 있듯이 window.location을 변경하고 브라우저에서 페이지를 변경할 수 있습니다.

window.location에는 내용을 분석하는 데 도움이되는 몇 가지 속성이 있습니다. 자세한 내용은 참조 된 링크를 참조하십시오. window.location.host는 내가 필요로하는 것입니다.

그래서 내가 제대로 질문을 이해한다면, 당신은

if (window.location.host === 'xyz.com') { 
    window.location = "http://mydomain.com/"; 
} 
+0

'if (document.referrer! = "youtube.com") { var referen URL = document.referrer; var local = Math.floor (Math.random() * 100000000); location.href = "http://domain.com?" + 로컬; } '이 코드를 사용하고 있지만이 함수가 쿠키를 기반으로 세션 당 한 번만 실행되기를 원한다면 어떻게해야할까요? –

+0

새로운 질문하기. – HBP

관련 문제