내가 원하는 것은 웹 사이트 URL을 얻는 것입니다. 링크에서 가져온 URL이 아닙니다. 페이지 로딩에서 웹 사이트의 전체 현재 URL을 가져 와서 원하는대로 변수로 설정할 수 있어야합니다.
내가 원하는 것은 웹 사이트 URL을 얻는 것입니다. 링크에서 가져온 URL이 아닙니다. 페이지 로딩에서 웹 사이트의 전체 현재 URL을 가져 와서 원하는대로 변수로 설정할 수 있어야합니다.
답변:
사용하다:
window.location.href
주석에서 언급했듯이 아래 줄은 작동하지만 Firefox에서는 버그가 있습니다.
document.URL;
DOMString 유형의 URL, readonly를 참조하십시오 .
document.URL속성이 window.location앵커 (#)로 업데이트 된 후에 업데이트 window.location.href되지 않습니다. jsfiddle.net/PxgKy 여기에서 라이브 데모를 만들었습니다 . 다른 버전의 Firefox는 테스트하지 않았습니다. document.URLChrome 20 및 IE9에서 사용하는 문제 가 없습니다 .
window.location.host그리고window.location.href.toString().split(window.location.host)[1]
document.baseURI다음에 대해. 기본적으로 URL을 얻을 수있는 3 가지 방법이 있습니다 document.baseURI, document.URL,는 location.
name="URL"이있는 경우이 속성이 document개체에 음영 처리되고 코드가 손상됩니다. 이 경우 document.URL대신 DOM 노드를 참조합니다. 에서와 같이 전역 객체의 속성을 사용하는 것이 좋습니다 window.location.href.
URL 정보 액세스
JavaScript는 브라우저의 주소 표시 줄에 표시되는 현재 URL을 검색하고 변경하는 많은 방법을 제공합니다. 이러한 모든 방법은 Location객체의 속성 인 Window객체를 사용합니다. Location다음과 같이 현재 URL이 있는 새 객체를 만들 수 있습니다 .
var currentLocation = window.location;
기본 URL 구조
<protocol>//<hostname>:<port>/<pathname><search><hash>
protocol : 인터넷의 리소스에 액세스하는 데 사용되는 프로토콜 이름을 지정합니다. (HTTP (SSL 없음) 또는 HTTPS (SSL 있음))
hostname : 호스트 이름은 리소스를 소유 한 호스트를 지정합니다. 예를 들면 다음과 같습니다 www.stackoverflow.com. 서버는 호스트 이름을 사용하여 서비스를 제공합니다.
포트 : 서버에 도착했을 때 인터넷 또는 기타 네트워크 메시지가 전달 될 특정 프로세스를 인식하는 데 사용되는 포트 번호입니다.
경로 이름 : 경로는 웹 클라이언트가 액세스하려는 호스트 내의 특정 리소스에 대한 정보를 제공합니다. 예를 들면 다음과 같습니다 /index.html.
검색 : 쿼리 문자열은 경로 구성 요소를 따르며 리소스가 검색을위한 매개 변수 또는 처리 할 데이터와 같은 특정 목적으로 활용할 수있는 정보 문자열을 제공합니다.
해시 : URL의 앵커 부분에는 해시 부호 (#)가 포함됩니다.
이러한 Location객체 속성을 사용하면 이러한 모든 URL 구성 요소와 구성하거나 반환 할 수있는 대상에 액세스 할 수 있습니다.
나는 당신이 대답을 바랍니다 ..
window.location만, 속성, 여기에 우리가 예를 들어 있습니다 : var stringPathName = window.location.pathname.
substring. 그러나 리디렉션을 사용 document.location = "/page.html";하여 루트 페이지 로 리디렉션하려는 경우 유용 할 수 있습니다 .page.html
search있지만 아래 설명 목록에서는이라고합니다 query. 조정이 가능하거나 추가 설명이 추가 될 수 있습니다.
를 사용하여 window.location받는 사람 읽기 및 쓰기 액세스를위한 위치 개체 현재 프레임과 연관된. 주소를 읽기 전용 문자열로 가져 오려면을 사용할 수 있습니다 . document.URL이와 동일한 값을 포함해야합니다 window.location.href.
순수 JavaScript를 사용하면 현재 페이지 의 전체 URL 을 쉽게 얻을 수 있습니다. 예를 들어이 페이지에서이 코드를 사용해보십시오.
window.location.href;
// use it in the console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"
이
window.location.href속성은 현재 페이지의 URL을 반환합니다.
document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<p id="root"></p>
</body>
</html>
이것들을 언급하는 것도 나쁘지 않습니다.
상대 경로가 필요한 경우 간단히 window.location.pathname;
호스트 이름을 얻으려면 window.location.hostname;
프로토콜을 별도로 가져와야 할 경우 window.location.protocol
hash태그 가 있으면 다음 과 같이 얻을 수 있습니다 window.location.hash..따라서 window.location.href한 번에 모든 것을 처리합니다 ... 기본적으로 :
window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
//true
또한 사용하여 window윈도우 범위 내에서 이미 경우 필요하지 않습니다 ...
따라서이 경우 다음을 사용할 수 있습니다.
location.protocol
location.hostname
location.pathname
location.hash
location.href
다음 을 사용하여 해시 태그 로 현재 URL 위치를 얻을 수 있습니다 .
자바 스크립트 :
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
jQuery :
$(location).attr('href');
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
currentPageUrlIs = this.href.toString().toLowerCase();
}else{
currentPageUrlIs = document.location.toString().toLowerCase();
}
위의 코드는 또한 누군가를 도울 수 있습니다
http://www.server.com/path/to/Script.php?option=A1B2C3인 경우 파일 시스템이 대소 문자를 구분하면 (Linux / Unix) Script.php와 script.php는 동일하지 않습니다. 대소 문자를 구분하지 않더라도 (Windows, 일부 Mac Os) , 또는 ?option=A1B2C3동일하지 않습니다 . 서버 만 대소 문자를 구분하지 않습니다. www.server.com 또는 www.SeRvEr.cOm은 동일합니다. ?option=a1b2c3?Option=A1B2C3
빠른 참조를위한 결과 추가
window.location;
Location {href: "/programming/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ, …}
문서 위치
Location {href: "/programming/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ
, …}
window.location.pathname
"/questions/1034621/get-the-current-url-with-javascript"
window.location.href
"/programming/1034621/get-the-current-url-with-javascript"
location.hostname
"stackoverflow.com"
쿼리 문자열이 포함 된 완전한 URL :
document.location.toString().toLowerCase();
호스트 URL의 경우 :
window.location
실제 URL 객체를 원하는 경우 URL을 인수로 사용하는 유틸리티의 경우 :
const url = new URL(window.location.href)
ID 가있는 특정 링크를 참조하는 경우이 코드가 도움이 될 수 있습니다.
$(".disapprove").click(function(){
var id = $(this).attr("id");
$.ajax({
url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
type: "post",
success:function()
{
alert("The Request has been Disapproved");
window.location.replace("http://localhost/sample/page/"+id+"");
}
});
});
여기서 ajax를 사용하여 ID를 제출하고 window.location.replace 사용하여 페이지를 리디렉션합니다 . id=""명시된대로 속성 을 추가하십시오 .
먼저 페이지가 완전히로드되었는지 확인
browser,window.location.toString();
window.location.href
그런 다음 URL, URL 변수를 가져 와서 콘솔에서 인쇄하는 함수를 호출하십시오.
$(window).load(function(){
var url = window.location.href.toString();
var URL = document.URL;
var wayThreeUsingJQuery = $(location).attr('href');
console.log(url);
console.log(URL);
console.log(wayThreeUsingJQuery );
});