[ 자바스크립트 ] 웹주소 쪼개기 2 🕔 2015. 6. 8. 08:25
prototype을 사용하여, 앞에서 만들었던 함수를, 다시 작성하였다.
<script type="text/javascript"> // 만든이 : http://tonks.tistory.com/91 function parsingURI ( uri ) { var regExp = /^([^:/?#]+:)?(\/{2})?([^/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/; // original source : /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/ splitUp = uri.match( regExp ); if ( splitUp == null ) return null; for ( var n = 0; n < splitUp.length; n++ ) if ( splitUp[ n ] == undefined ) splitUp[ n ] = ''; this.uri = function () { return splitUp[ 0 ]; }; this.protocol = function () { return splitUp[ 1 ]; }; this.slashes = function () { return splitUp[ 2 ]; }; this.host = function () { return splitUp[ 3 ]; }; this.pathname = function () { return splitUp[ 4 ]; }; this.search = function () { return splitUp[ 5 ]; }; this.hash = function () { return splitUp[ 6 ]; }; this.origin = function () { return this.protocol() + this.slashes() + this.host(); }; this.hostname = function () { return this.host().split( /:\d*$/ )[0]; }; this.port = function () { var portNumber = /\d*$/.exec ( this.host() ); if ( portNumber == null ) return ''; return portNumber; }; } parsingURI.prototype.encoding = function () { for ( var n = 0; n < splitUp.length; n++ ) splitUp[ n ] = encodeURI( splitUp[ n ] ); }; parsingURI.prototype.decoding = function () { for ( var n = 0; n < splitUp.length; n++ ) splitUp[ n ] = decodeURI( splitUp[ n ] ); }; </script>
변환하기 ( encoding )
full URI :::
protocol of URI :::
host of URI :::
pathname of URI :::
search of URI :::
hash of URI :::
origin of URI :::
hostname of URI :::
port of URI :::
변환하기 ( decoding )
full URI :::
protocol of URI :::
host of URI :::
pathname of URI :::
search of URI :::
hash of URI :::
origin of URI :::
hostname of URI :::
port of URI :::
웹브라우저에서 읽어들인 값으로 가져오기
<p id="uri"></p>
<p id="protocol"></p>
<p id="slashes"></p>
<p id="host"></p>
<p id="pathname"></p>
<p id="origin"></p>
<p id="hostname"></p>
<script type="text/javascript"> // ──────── Get the value read from the Web browser. ───────────────────── function get_value_of_browser () { var separate = new parsingURI ( location.href ); var uri = separate.uri(); var protocol = separate.protocol(); var slashes = separate.slashes(); var host = separate.host(); var pathname = separate.pathname(); var querystring = separate.search(); var hash = separate.hash(); var origin = separate.origin(); var hostname = separate.hostname(); var port = separate.port(); document.getElementById( "uri" ) .innerHTML = uri; document.getElementById( "protocol" ) .innerHTML = protocol; document.getElementById( "slashes" ) .innerHTML = slashes; document.getElementById( "host" ) .innerHTML = host; document.getElementById( "pathname" ).innerHTML = pathname; document.getElementById( "origin" ) .innerHTML = origin; document.getElementById( "hostname" ).innerHTML = hostname; } </script>
웹 브라우저의 값을 변환하기 ( encoding )
<p id="enUri"></p>
<p id="enProto"></p>
<p id="enSlash"></p>
<p id="enHost"></p>
<p id="enPath"></p>
<p id="enOrig"></p>
<p id="enHnm"></p>
<script type="text/javascript"> // ──────── 변환하기 ( encoding ) ────────────────────────────── function encode () { var separate = new parsingURI ( location.href ); separate.encoding(); var uri = separate.uri(); var protocol = separate.protocol(); var slashes = separate.slashes(); var host = separate.host(); var pathname = separate.pathname(); var origin = separate.origin(); var hostname = separate.hostname(); document.getElementById( "enUri" ) .innerHTML = uri; document.getElementById( "enProto" ) .innerHTML = protocol; document.getElementById( "enSlash" ) .innerHTML = slashes; document.getElementById( "enHost" ) .innerHTML = host; document.getElementById( "enPath" ) .innerHTML = pathname; document.getElementById( "enOrig" ) .innerHTML = origin; document.getElementById( "enHnm" ) .innerHTML = hostname; } </script>
웹 브라우저의 값을 변환하기 ( decoding )
<p id="deUri"></p>
<p id="deProto"></p>
<p id="deSlash"></p>
<p id="deHost"></p>
<p id="dePath"></p>
<p id="deOrig"></p>
<p id="deHnm"></p>
<script type="text/javascript"> // ──────── 변환하기 ( decoding ) ────────────────────────────── function decode () { var separate = new parsingURI ( location.href ); separate.decoding(); var uri = separate.uri(); var protocol = separate.protocol(); var slashes = separate.slashes(); var host = separate.host(); var pathname = separate.pathname(); var origin = separate.origin(); var hostname = separate.hostname(); document.getElementById( "deUri" ) .innerHTML = uri; document.getElementById( "deProto" ) .innerHTML = protocol; document.getElementById( "deSlash" ) .innerHTML = slashes; document.getElementById( "deHost" ) .innerHTML = host; document.getElementById( "dePath" ) .innerHTML = pathname; document.getElementById( "deOrig" ) .innerHTML = origin; document.getElementById( "deHnm" ) .innerHTML = hostname; } </script>
이 글의 주소 (href of this post) ::: http://tonks.tistory.com/91
이 내용이 도움이 되셨나요? *^^*
'JS 실습하기 > 유니코드' 카테고리의 다른 글
“한글 인코딩” 관련 사이트 목록 (0) | 2015.06.24 |
---|---|
[ 자바스크립트 ] 웹주소 쪼개기 1 (0) | 2015.06.03 |
쓸만한 유니코드 모음 (0) | 2015.01.05 |
문자를 유니코드로 변환하기 (0) | 2015.01.02 |