상세 컨텐츠

본문 제목

[JavaScript] 자바스크립트 현재 페이지 주소 얻기 관련

WEB/JavaScript

by AlrepondTech 2017. 11. 23. 15:31

본문

반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

출처: http://mwultong.blogspot.com/2006/09/html-javascript-current-page-url.html

 

 

다음과 같은 자바스크립트로, 현재 페이지의 주소, 즉 브라우저 주소창의 주소(URL)를 얻을 수 있습니다.

<script type="text/javascript">

// 곧바로 현재 페이지 주소 출력
document.writeln(location.href);


// 변수에 넣어서 출력
var s = location.href;
document.writeln(s);

</script>


location.href 속에 현재 주소가 들어 있습니다.


예를 들어, 위의 자바스크립트 코드가 들어 있는 페이지의 주소가
http://www.google.com/test.html
이라면, 화면에는

http://www.google.com/test.html http://www.google.com/test.html


이렇게 출력됩니다.

 

 

 

=================================

=================================

=================================

 

 

 

 

반응형

 

 

728x90

 

 

 

 

출처: http://88240.tistory.com/36

 

javascript로 현재 페이지의 url 가져오는 방법은 다음과 같다.

var link =  document.location.href; console.log(link);

1번째 라인은 link라는 변수에 현재 페이지의 url을 가져와 선언한다.

2번째 라인은 선언된 값을 콘솔에 출력한다.

위의 소스를 실행했을 때,

예를들어 현재 페이지의 url이 88240.tisory.com이라면 88240.tisory.com가 출력될 것이다.

 

그렇다면 88240.tisory.com?page=1 이렇게 파라미터를 포함하고 있는 url에서 파라미터값만 가져오고 싶을 경우는?

다음과 같이 쓰면된다.

var para = document.location.href.split("?"); console.log(para);

 

하면 url에서 ?뒤에 붙은 값만 가져온다. 

위에 들었던 예시대로라면 page=1만 콘솔에 출력될 것이다.

즉, 파라미터값만 출력된다는 것.



출처: http://88240.tistory.com/36 [shaking blog]

 

 

=================================

=================================

=================================

 

 

 

출처: http://beone.tistory.com/573

 

 

실행url - http://localhost:8088/login/login.do?key=value

 

 

-javascript-

location.href -> http://localhost:8088/login/login.do?key=value

location.protocol -> http:
location.host -> localhost:8088 
location.pathname -> /login/login.do
location.search -> ?key=value

 

 

-jquery-

jQuery(location).attr('href') -> http://localhost:8088/login/login.do?key=value
jQuery(location).attr('protocol') -> http:
jQuery(location).attr('host') -> localhost:8088 
jQuery(location).attr('pathname') -> /login/login.do
jQuery(location).attr('search')-> ?key=  
jQuery(location).attr('search')-> ?key=

 

 



출처: http://beone.tistory.com/573 [I LOVE EJ]

 

 

=================================

=================================

=================================

 

 

 

반응형


관련글 더보기

댓글 영역