본문 바로가기

[IT/Programming]/HTML related

Encode/Unescape and Decode/Escape URI Component

반응형
m.logPrint() is working!

<eq> and <eqq> tags are rendered to MathJax format, being enclosed by \ ( \ ) and \ [ \ ].

docuK1 scripts started!
If this log is not closed automatically, there must be an error somewhere in your document or scripts.

Table of Contents is filled out.

Auto numberings of sections (div.sec>h2, div.subsec>h3, div.subsubsec>h4), <eqq> tags, and <figure> tags are done.

<cite> and <refer> tags are rendered to show bubble reference.

<codeprint> tags are printed to corresponding <pre> tags, only when the tags exist in the document.


Current styles (dark/bright mode, font-family, font-size, line-height) are shown.

disqus.js with id="disqus-js" is loaded.

kakao.js with id="kakao-js-sdk" is loaded.

New ShortKeys (T: Table of Contents, F: Forward Section, D: Previous Section, L: To 전체목록/[Lists]) are set.

m.delayPad=512;
m.wait=1024;
wait 753ms.
Doing delayed-load. : 1
▼ Hide
Toggle a mess
Go (FS)
TofC
DocuK Log
Backward
Forward
RRA
Lists
CmtZ
CmtX
Handle CmtZ
Log in
out focus
현재 docuK SEE 3.0.0 버전에 에러가 있어서 글이 제대로 안보일겁니다. 곧 고칠테니 기다려주세요.
이 글이 도움이 되셨다면, 광고 클릭 한번씩만 부탁드립니다 =ㅂ=ㅋ. (If this article was helpful, please click the ad once. Thank you. ;)
Mode: Bright; Font: Noto Sans KR; font-size: 18.0px (10.0); line-height: 1.6;
width: 1280, height: 720, version: 3.0.0
Canonical URI: https://kipid.tistory.com/entry/Encode-Unescape-and-Decode-Escape-URI-Component
dg:plink (Document Global Permanent Link): https://kipid.tistory.com/126
document.referrer: Empty
This document is rendered by docuK (See also SEE (Super Easy Edit) of docuK and pure SEE).

Encode/Unescape and Decode/Escape URI Component

http 주소창에 space 나 한글, 특수문자 같은 것들이 낑겨 있을 경우, 여러모로 에러가 날 가능성이 높아져서 보통 브라우저들이 이런 문자들은 '%AB' 같은 8 bit (?) 어쩌구로 변환시켜 버린다. Javascript 에서 기본적으로 이런것들을 처리하는 encodeURIComponent, decodeURIComponent 함수들을 제공한다.
뭐 쓰는 사람은 알아서 공부해서 쓸테니... 인터넷 돌아다니다가 이상한 주소가 실제 어떤 주소인지 궁금할때 이용하시라고 (+내가 쓸라고) 만들어 봄.

TdocuK0-sec-PH0.Posting History

▼ Show/Hide

T1.Decode URI Component

▼ Show/Hide
한글같은 경우 3 byte 가 한묶음이라, 제대로 조합이 안맞으면 decode error 남.
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
text-decode-result
▲ Hide

T2.Encode URI Component

▼ Show/Hide
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
text-encode-result
▲ Hide

T3.Codes

▼ Show/Hide

T3.1.Escape codes manually coded.

////////////////////////////////////////////////////
// Escape and Unescape HTML string.
////////////////////////////////////////////////////
m.escapeHTML=function (str) {
	if (!str||str.constructor!==String) { return ""; }
	return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
m.escapeOnlyTag=function (str) {
	if (!str||str.constructor!==String) { return ""; }
	return str.replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
m.unescapeHTML=function (str) {
	if (!str||str.constructor!==String) { return ""; }
	return str.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&amp;/g,'&');
};
m.escapeAMP=function (str) {
	if (!str||str.constructor!==String) { return ""; }
	return str.replace(/%/g,'%25').replace(/&/g,'%26').replace(/#/g,'%23');
};
m.unescapeAMP=function (str) {
	if (!str||str.constructor!==String) { return ""; }
	return str.replace(/%23/g,'#').replace(/%26/g,'&').replace(/%25/g,'%');
};

T3.2.Using existing functions.

On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
<script>
(function() {
	$("#text-decode").on("input", function(e) {
		let $result=$("#text-decode-result");
		let str="";

		str+="unescape :\n";
		try {
			str+=unescape(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\ndecodeURI :\n";
		try {
			str+=decodeURI(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\ndecodeURIComponent :\n";
		try {
			str+=decodeURIComponent(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\nm.unescapeHTML :\n";
		str+=m.unescapeHTML(this.value);

		str+="\n\nm.unescapeAMP :\n";
		str+=m.unescapeAMP(this.value);

		$result.html(m.escapeHTML(str));
		$result.removeClass("prettyprinted");
		PR.prettyPrint(function(){}, $result.parent()[0]);
	});

	$("#text-encode").on("input", function(e) {
		let $result=$("#text-encode-result");
		let str="";

		str+="escape :\n";
		try {
			str+=escape(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\nencodeURI :\n";
		try {
			str+=encodeURI(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\nencodeURIComponent :\n";
		try {
			str+=encodeURIComponent(this.value);
		} catch (e) {
			str+=e;
		}

		str+="\n\nm.escapeHTML :\n";
		str+=m.escapeHTML(this.value);

		str+="\n\nm.escapeOnlyTag :\n";
		str+=m.escapeOnlyTag(this.value);

		str+="\n\nm.escapeAMP :\n";
		str+=m.escapeAMP(this.value);

		$result.html(m.escapeHTML(str));
		$result.removeClass("prettyprinted");
		PR.prettyPrint(function(){}, $result.parent()[0]);
	});
})();
</script>
▲ Hide
현재 docuK SEE 3.0.0 버전에 에러가 있어서 글이 제대로 안보일겁니다. 곧 고칠테니 기다려주세요.
이 글이 도움이 되셨다면, 광고 클릭 한번씩만 부탁드립니다 =ㅂ=ㅋ. (If this article was helpful, please click the ad once. Thank you. ;)
반응형
Get page views