{"id":3361,"date":"2013-10-15T09:00:31","date_gmt":"2013-10-15T00:00:31","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3361"},"modified":"2018-11-14T16:33:53","modified_gmt":"2018-11-14T07:33:53","slug":"javascript%e3%81%ae%e3%82%a8%e3%82%b9%e3%82%b1%e3%83%bc%e3%83%97%e3%81%82%e3%82%8c%e3%81%93%e3%82%8c","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/10\/15\/javascript%e3%81%ae%e3%82%a8%e3%82%b9%e3%82%b1%e3%83%bc%e3%83%97%e3%81%82%e3%82%8c%e3%81%93%e3%82%8c\/","title":{"rendered":"JavaScript\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u3042\u308c\u3053\u308c"},"content":{"rendered":"<p>\u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>\u305f\u307e\u306b\u306fRuby\u4ee5\u5916\u306e\u8a18\u4e8b\u3082\u66f8\u3044\u3066\u307f\u3088\u3046\u3068\u601d\u3063\u305f\u306e\u3067\u3001<br \/>\n\u77e5\u3063\u3066\u304a\u304d\u305f\u3044JavaScript\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u51e6\u7406\u306b\u95a2\u3059\u308b\u5c0f\u30cd\u30bf\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h2>encodeURI, decodeURI \u306f\u4f7f\u308f\u306a\u3044<\/h2>\n<p>JavaScript\u3067URL\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u884c\u3046\u5834\u5408\u306b\u3001encodeURI, decodeURI\u3092\u4f7f\u7528\u3057\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002<br \/>\n\u3053\u308c\u3089\u306e\u95a2\u6570\u306f\u300c:\u300d\u3084\u300c\/\u300d\u306a\u3069\u306e\u3001URL\u4e0a\u3067\u610f\u5473\u3092\u6301\u3064\u8a18\u53f7\u306f\u51e6\u7406\u3057\u3066\u304f\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>encodeURIComponent, decodeURIComponent\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u5b9f\u4f8b\u3067\u898b\u3066\u307f\u307e\u3059\u3002<br \/>\n\u300c\u3042\u3044\u3046\u3048\u304a\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u5bfe\u8c61\u3068\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u7d50\u679c\u306f\u540c\u3058\u3067\u3059\u304c\u3001<br \/>\n\u300chttp:\/\/\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u5bfe\u8c61\u3068\u3057\u305f\u5834\u5408\u3001\u7d50\u679c\u304c\u7570\u306a\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:javascript decode:true \" >\r\nvar value;\r\n\r\n\/\/ -------------------------\r\n\/\/ \u3072\u3089\u304c\u306a\u306eURL\u30a8\u30f3\u30b3\u30fc\u30c9\r\n\/\/ -------------------------\r\n\r\nvalue = \"\u3042\u3044\u3046\u3048\u304a\";\r\n\/\/ \u3069\u3061\u3089\u3082\u300chttp:\/\/example.com\/%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A\/hoge\u300d\u306b\u306a\u308b\r\nconsole.log(\"http:\/\/example.com\/\" + encodeURI(value) + \"\/hoge\"); \/\/ \r\nconsole.log(\"http:\/\/example.com\/\" + encodeURIComponent(value) + \"\/hoge\");\r\n\r\n\/\/ -------------------------\r\n\/\/ \u3072\u3089\u304c\u306a\u306eURL\u30c7\u30b3\u30fc\u30c9\r\n\/\/ -------------------------\r\n\r\nvalue = encodeURIComponent(\"\u3042\u3044\u3046\u3048\u304a\");\r\n\/\/ \u3069\u3061\u3089\u3082\u300c\u3042\u3044\u3046\u3048\u304a\u300d\u306b\u306a\u308b\r\nconsole.log(decodeURI(value));\r\nconsole.log(decodeURIComponent(value));\r\n\r\n\r\n\/\/ -------------------------\r\n\/\/ URL\u8a18\u53f7\u306eURL\u30a8\u30f3\u30b3\u30fc\u30c9\r\n\/\/ -------------------------\r\n\r\nvalue = \"http:\/\/\";\r\n\/\/ \u300chttp:\/\/example.com\/http:\/\/\/hoge\u300d\u306b\u306a\u308b\u3002\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u3066\u3044\u306a\u3044\u3002\r\nconsole.log(\"http:\/\/example.com\/\" + encodeURI(value) + \"\/hoge\");\r\n\/\/ \u300chttp:\/\/example.com\/http%3A%2F%2F\/hoge\u300d\u306b\u306a\u308b\u3002OK!\r\nconsole.log(\"http:\/\/example.com\/\" + encodeURIComponent(value) + \"\/hoge\");\r\n\r\n\/\/ -------------------------\r\n\/\/ URL\u8a18\u53f7\u306eURL\u30c7\u30b3\u30fc\u30c9\r\n\/\/ -------------------------\r\n\r\nvalue = encodeURIComponent(\"http:\/\/\");\r\n\/\/ \u300chttp%3A%2F%2F\u300d\u306b\u306a\u308b\u3002\u30c7\u30b3\u30fc\u30c9\u3055\u308c\u3066\u3044\u306a\u3044\u3002\r\nconsole.log(decodeURI(value));\r\n\/\/ \u300chttp:\/\/\u300d\u306b\u306a\u308b\u3002OK!\r\nconsole.log(decodeURIComponent(value));\r\n<\/pre>\n<p>encodeURI\u3084decodeURI\u3092\u5229\u7528\u3057\u306a\u3044\u3068\u4e0a\u624b\u304f\u3044\u304b\u306a\u3044\u3068\u3044\u3046\u5834\u5408\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u69cb\u9020\u3092\u9593\u9055\u3048\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u975e\u5e38\u306b\u9ad8\u3044\u306e\u3067\u3001\u4e00\u5ea6\u898b\u76f4\u3057\u3066\u307f\u308b\u3068\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>HTML\u3092\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u305f\u3044<\/h2>\n<p>JavaScript\u3067DOM\u3092\u4f7f\u3063\u3066HTML\u3092\u7d44\u307f\u7acb\u3066\u308b\u306e\u306f\u9762\u5012\u3067\u3059\u3002<\/p>\n<pre class=\"lang:javascript decode:true \" >\r\n\/\/ DOM\u3092\u4f7f\u3063\u3066body\u306b<div attr1=\"hoge\">\u3042\u3044\u3046\u3048\u304a<\/div>\u3092\u8ffd\u52a0\u3059\u308b\r\nvar attr1   = \"hoge\";\r\nvar content = \"\u3042\u3044\u3046\u3048\u304a\";\r\n\r\nvar body = document.getElementsByTagName('body')[0];\r\nvar div = document.createElement('div');\r\ndiv.setAttribute(\"attr1\", attr1);\r\ndiv.innerText(content);\r\nbody.appendChild(div);\r\n<\/pre>\n<p>\u3053\u3093\u306a\u6642\u306finnerHTML\u3084jQuery\u3092\u4f7f\u3063\u3066\u4e00\u6c17\u306b\u7d44\u307f\u7acb\u3066\u3066\u3057\u307e\u3044\u305f\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:javascript decode:true \" >\r\n\/\/ \u6587\u5b57\u5217\u3067\u7d44\u307f\u7acb\u3066\u3066body\u306b<div attr1=\"hoge\">\u3042\u3044\u3046\u3048\u304a<\/div>\u3092\u8ffd\u52a0\u3059\u308b\r\nvar attr1   = \"hoge\";\r\nvar content = \"\u3042\u3044\u3046\u3048\u304a\";\r\n\r\n# innerHTML\r\nbody.innerHTML = '<div attr1=\"' + attr1 + '\">' + content + '<\/div>';\r\n# jQuery\r\n$('body').html('<div attr1=\"' + attr1 + '\">' + content + '<\/div>');\r\n<\/pre>\n<p>\u3057\u304b\u3057\u3001attr1\u3084content\u306b\u30bf\u30b0\u306b\u4f7f\u308f\u308c\u308b\u8a18\u53f7\u3067\u3042\u308b\u300c<>\"'\u300d\u306a\u3069\u304c\u304d\u305f\u5834\u5408\u3001HTML\u3092\u58ca\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3092\u56de\u907f\u3059\u308b\u305f\u3081\u306b\u3001\u30bf\u30b0\u306b\u57cb\u3081\u8fbc\u3080\u6587\u5b57\u5217\u306fHTML\u306e\u5b9f\u4f53\u53c2\u7167\u306b\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\nPHP\u3067\u3044\u3046htmlspecialchars\u3067\u3059\u306d\u3002<\/p>\n<p>\u6b8b\u5ff5\u306a\u304c\u3089JavaScript\u306b\u306f\u3053\u306e\u52d5\u4f5c\u3092\u884c\u3046\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u304c\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u307e\u3059\u3002<\/p>\n<pre class=\"lang:javascript decode:false\" >\r\n\/\/ HTML\u30a8\u30b9\u30b1\u30fc\u30d7\r\nfunction escapeHTML(html) {\r\n  var elem = document.createElement('div');\r\n  elem.appendChild(document.createTextNode(html));\r\n  return elem.innerHTML;\r\n}\r\n\r\n\/\/ HTML\u30a8\u30b9\u30b1\u30fc\u30d7 - jQuery\u3092\u4f7f\u3046\u3068\u4e00\u884c\u3067\u66f8\u3051\u308b\r\nfunction escapeHTML2(html) {\r\n  return jQuery('<div>').text(html).html();\r\n}\r\n\r\n\/\/ \u3069\u3061\u3089\u3082 \u300c&lt;a&gt;\u300d \u306b\u306a\u308b\r\nconsole.log(escapeHTML('<a>'));\r\nconsole.log(escapeHTML2('<a>'));\r\n<\/pre>\n<p>\u4e8c\u3064\u306e\u95a2\u6570\u306e\u3069\u3061\u3089\u3082\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u540c\u3058\u3067\u3001\u4ee5\u4e0b\u306e\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ol>\n<li>\u30c0\u30df\u30fc\u306ediv\u8981\u7d20\u3092\u4f5c\u6210<\/li>\n<li>\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u305f\u3044\u6587\u5b57\u3092\u30bf\u30b0\u5185\u306e\u6587\u5b57\u5217\u306b\u8a2d\u5b9a<\/li>\n<li>div\u8981\u7d20\u5185\u306eHTML\u3092\u53d6\u308a\u51fa\u3059<\/li>\n<\/ol>\n<p>\u6b63\u898f\u8868\u73fe\u3067\u7f6e\u63db\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u3053\u306e\u65b9\u6cd5\u3060\u3068JavaScript\u30a8\u30f3\u30b8\u30f3\u306b<br \/>\n\u51e6\u7406\u3092\u4efb\u305b\u3089\u308c\u308b\u306e\u3067\u30b9\u30de\u30fc\u30c8\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\n2013\/4\/18 \u8ffd\u8a18<br \/>\n<a href=\"http:\/\/qiita.com\/noriaki\/items\/4bfef8d7cf85dc1035b3\">\u3053\u3061\u3089\u306e\u8a18\u4e8b<\/a>\u306b\u3042\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u65b9\u6cd5\u3067\u306f\u30bf\u30b0\u5185\u306e\u30c6\u30ad\u30b9\u30c8\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u306b\u306f\u6709\u52b9\u3067\u3059\u304c\u3001\u30bf\u30b0\u306e\u5c5e\u6027\u5024\u3067\u306f\u300c\"\u300d\u300c'\u300d\u304c\u30a8\u30b9\u30b1\u30fc\u30d7\u3055\u308c\u306a\u3044\u305f\u3081\u5371\u967a\u3067\u3059\u3002<br \/>\n\u5c5e\u6027\u5024\u306e\u5834\u5408\u306f\u6b63\u898f\u8868\u73fe\u306a\u3069\u3067\u7f6e\u63db\u3059\u308b\u304b\u3001JavaScript\u304b\u3089DOM\u306e\u5c5e\u6027\u306b\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3079\u304d\u3067\u3057\u3087\u3046\u3002<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>\u305f\u307e\u306b\u306fRuby\u4ee5\u5916\u306e\u8a18\u4e8b\u3082\u66f8\u3044\u3066\u307f\u3088\u3046\u3068\u601d\u3063\u305f\u306e\u3067\u3001<br \/>\n\u77e5\u3063\u3066\u304a\u304d\u305f\u3044JavaScript\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u51e6\u7406\u306b\u95a2\u3059\u308b\u5c0f\u30cd\u30bf\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/10\/15\/javascript%e3%81%ae%e3%82%a8%e3%82%b9%e3%82%b1%e3%83%bc%e3%83%97%e3%81%82%e3%82%8c%e3%81%93%e3%82%8c\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3361"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=3361"}],"version-history":[{"count":2,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3361\/revisions"}],"predecessor-version":[{"id":10524,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3361\/revisions\/10524"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}