programing

jquery, 도메인, URL 가져오기

magicmemo 2023. 8. 14. 22:46
반응형

jquery, 도메인, URL 가져오기

jquery로 도메인 이름을 얻으려면 어떻게 해야 합니까?

간단한 Javascript로 충분하므로 이를 위해 jQuery가 필요하지 않습니다.

alert(document.domain);

실제로 보기:

console.log("Output;");  
console.log(location.hostname);
console.log(document.domain);
alert(window.location.hostname)

console.log("document.URL : "+document.URL);
console.log("document.location.href : "+document.location.href);
console.log("document.location.origin : "+document.location.origin);
console.log("document.location.hostname : "+document.location.hostname);
console.log("document.location.host : "+document.location.host);
console.log("document.location.pathname : "+document.location.pathname);

추가 도메인 관련 값을 보려면 온라인 속성을 확인하십시오.는 것을 발견할 수 있습니다.location.host내용이 다를 수 있기 때문에 더 나은 선택입니다.document.domain예를 들어 url은http://192.168.1.80:8080IP 주소만 다음에 포함됩니다.document.domain하지만 IP 주소와 포트 번호 모두.location.host.

편집:

IE10을 지원할 필요가 없다면 다음을 사용하면 됩니다.document.location.origin

기존 지원이 필요한 경우 원본 답변

위치 개체를 검사하면 다음과 같은 모든 정보를 얻을 수 있습니다.

location = {
  host: "stackoverflow.com",
  hostname: "stackoverflow.com",
  href: "http://stackoverflow.com/questions/2300771/jquery-domain-get-url",
  pathname: "/questions/2300771/jquery-domain-get-url",
  port: "",
  protocol: "http:"
}

그래서:

location.host 

도메인이 될 수 있습니다. 이 경우 stackoverflow.com .URL의 전체 첫 번째 부분에 대해 다음을 사용할 수 있습니다.

location.protocol + "//" + location.host

이 경우에는 http://stackoverflow.com 이 될 것입니다.

jQuery가 필요하지 않습니다.

이전의 답과 유사합니다.

location.host

위치 전역에는 현재 URL에 대한 재미있는 사실도 있습니다. (프로토콜, 호스트, 포트, 경로 이름, 검색, 해시 )

만약 저처럼 문자열이 필요하다면, 이 기능을 사용하세요 - 정말 효과가 있습니다.

function getHost(url)
{
    var a = document.createElement('a');
    a.href = url;
    return a.hostname;
}

그러나 URL에 하위 도메인(예: www.)이 있으면 호스트 이름과 함께 반환됩니다.반대로 하위 도메인이 없으면 호스트 이름에도 하위 도메인이 없습니다.

아래 코드를 사용하여 현재 URL의 다른 매개 변수를 가져올 수 있습니다.

alert("document.URL : "+document.URL);
alert("document.location.href : "+document.location.href);
alert("document.location.origin : "+document.location.origin);
alert("document.location.hostname : "+document.location.hostname);
alert("document.location.host : "+document.location.host);
alert("document.location.pathname : "+document.location.pathname);

jQuery가 필요하지 않습니다. 간단한 javascript를 사용하십시오.

document.domain

document.baseURI도메인 + 포트를 제공합니다.이미지 태그가 절대 경로 대신 상대 경로를 사용하는 경우 사용됩니다.이미 해결되었을 수도 있지만, 다른 남자들에게 유용할 수도 있습니다.

var part = location.hostname.split('.');
var subdomains = part.shift();
var upperleveldomains = part.join('.');

사용할 수 있는 두 번째 수준 도메인

var sleveldomain = parts.slice(-2).join('.');

이것을 확인합니다.

alert(window.location.hostname);

호스트 이름이 www.domain.com 으로 반환됩니다.

//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-z]+.)/,"");
}
//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
}

언급URL : https://stackoverflow.com/questions/2300771/jquery-domain-get-url

반응형