development

jquery, 도메인, URL 얻기

big-blog 2020. 6. 22. 07:18
반응형

jquery, 도메인, URL 얻기


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


간단한 자바 스크립트로 충분하므로 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);

추가 도메인 관련 값은 window.location온라인 속성을 확인하십시오 . location.host내용이과 다를 수 있으므로 더 나은 옵션 임을 알 수 있습니다 document.domain. 예를 들어, URL http://192.168.1.80:8080에는 ipaddress 만 document.domain있고 ipaddress와 포트 번호는 모두에 location.host있습니다.


location 객체를 검사하여이 모든 것을 얻을 수 있습니다.

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가 필요하지 않습니다. 간단한 자바 스크립트를 사용하십시오.

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 you want to know the "registered domain", check this out: http://lbolla.wordpress.com/2011/04/05/get-registered-domain-in-python-and-javascript/

I've implemented reg-dom-libs (http://www.dkim-reputation.org/regdom-libs/) in Javascript and Python.


//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

반응형