BOM 객체를 통해 URL 가져오기

3117 단어
오늘 프로젝트를 하려면 다른 페이지에서 보내온 값을 이용해야 하기 때문에 지금 총괄해 봅시다.
URL을 얻기 위해 BOM의 위치 대상을 사용합니다
다음 예는 다음과 같습니다.
웹 URL 주소:
localhost/hzx/chinahpo/redirect.html?type=0&name=HP:0000246
js 코드:
console.log(document.location);
콘솔 출력:
Location
ancestorOrigins:DOMStringList assign:() hash:""host:"localhost"hostname:"localhost"href:"http://localhost/hzx/chinahpo/redirect.html?type=0&name=HP:0000246"origin:"http://localhost"pathname:"/hzx/chinahpo/redirect.html"port:""protocol:"http:"reload:reload() replace:() search:
"?type=0&name=HP:0000246"
획득한 값을 추출하는 방법:
var strUrl=document.location.search;
var data=(strURL).split('&');//['?type=0','name=HP:0000246']
var type=(data[0].split('='))[1];//0
var name=(data[1].split('='))[1];//HP:0000246
사실 완전한 URL을 직접 가져오는 다른 방법도 있습니다.
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;

좋은 웹페이지 즐겨찾기