이 안내서는 jQuery 코드 스 니펫을 제공하여 현재 웹 페이지의 전체 URL을 가져 와서 다른 스크립트와 함께 사용할 변수에 저장합니다. 이 URL은 주소 표시 줄에 표시된 URL과 동일합니다.
faqs (faqs)
$(document).ready(function() { // 使用jQuery var url = $(location).attr('href'); // 使用纯JavaScript var url2 = window.location.href; // 在警告窗口中显示URL alert(url); //或者 alert(url2); });
이 코드는 현재 URL을 변수에 저장하고 저장 한 다음이 URL을 콘솔에 인쇄합니다.
function getAbsolutePath() { var loc = window.location; var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1); return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length)); }
이 코드는 URL의 소스 (프로토콜, 호스트 이름 및 포트) 및 PathName을 가져 와서이 두 부분을 연결하여 쿼리 문자열이 포함되지 않은 URL을 형성합니다.
이 코드는 쿼리 문자열을 가져와 변수에 저장 한 다음이 쿼리 문자열을 콘솔에 인쇄합니다. window.location.href
$(document).ready(function(){ var currentURL = window.location.href; console.log(currentURL); });
currentURL
이 코드는 현재 URL을 가져 와서 해시 기호 (#)로 나눕니다. console.log(currentURL)
부분은 분할 URL의 첫 번째 부분, 즉 해시 값을 포함하지 않는 URL을 가져옵니다.
window.location.origin
이 코드는 해시 값을 가져 와서 변수에 저장 한 다음이 해시 값을 콘솔에 인쇄합니다. window.location.pathname
$(document).ready(function(){ var currentURL = window.location.origin + window.location.pathname; console.log(currentURL); });
이 코드는 페이지를 다시로드하지 않고 현재 URL을
window.location.protocol
이 코드는 프로토콜을 가져 와서
$(document).ready(function() { // 使用jQuery var url = $(location).attr('href'); // 使用纯JavaScript var url2 = window.location.href; // 在警告窗口中显示URL alert(url); //或者 alert(url2); });
protocol
속성을 사용하여 현재 URL의 호스트 이름을 얻을 수 있습니다. 이 속성은 URL의 호스트 이름을 반환합니다. 예는 다음과 같습니다. console.log(protocol)
속성을 사용하여 현재 URL의 포트를 얻을 수 있습니다. 이 속성은 URL의 포트를 반환합니다. 예는 다음과 같습니다. window.location.hostname
function getAbsolutePath() { var loc = window.location; var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1); return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length)); }
hostname
console.log(hostname)
속성을 사용하여 현재 URL의 경로 이름을 얻을 수 있습니다. 이 속성은 URL의 경로 이름을 반환합니다. 예는 다음과 같습니다.
위 내용은 jQuery는 현재 페이지 URL을 얻습니다의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!