javascript url 파싱 / 주소, 경로, 파라미터

HYEONG HWAN, MUN/ 10월 18, 2014/ 미분류/ 1 comments

https://blog.lael.be/post/87

location.protocol : http:
location.host : lael.be (주소)
location.pathname : board/bo_table.php (경로)
location.search : ?board_id=freeboard&seqs=24501&page=1&IsSearch= (파라미터)

응용소스

login_member 이면 진행, 아니면 로그인 페이지로 이동하는데 url 파라미터를 가지고 이동

1
2
3
4
5
6
7
8
9
function buy_this(){
if(! login_member()){
 alert('회원전용입니다.');
 if(confirm('로그인페이지로 이동하시겠습니까?')){
 location.href='/login.jsp?ret='+encodeURIComponent(location.pathname+location.search);
 }
 return false;
 }
}

 

응용소스2

get파라미터 값을 얻어오는 소스

정규식 설명 : 앞에 ?& 두개중 하나가 있고 + 파라미터명= + 값(&# 제외)

 

1
2
3
4
5
6
7
8
9
10
11
function get_param_value( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

1 Comment

Leave a Comment

작성하신 댓글은 관리자의 수동 승인 후 게시됩니다.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*