最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to decode base64 html inside HTML? - Stack Overflow

programmeradmin0浏览0评论

Sorry but I've been searching everywhere for a solution and I haven't found anything, so I have no idea on how to solve this.

I have an encoded base64 which I need to place inside a html file and then decode it when the page loads. I've seen before that it is possible, but I can't remember how to do that.

This is my encoded code:

PGRpdiBpZD0idGhlZGl2Ij4NCjxpbWcgc3JjPSJodHRwczovL21lZGlhLmdpcGh5LmNvbS9tZWRpYS9oTTg3RE1ubHM1b1p5L2dpcGh5LmdpZiIvPg0KPGEgcmVsPSJodHRwOi8vZ29vZ2xlLmNvbSI+R29vZ2xlIElOQzwvYT48YnIgLz4NCjwhLS08aW1nIHNyYz0iaHR0cHM6Ly9tZWRpYS5naXBoeS5jb20vbWVkaWEvaE04N0RNbmxzNW9aeS9naXBoeS5naWYiLz4tLT4NCjwvZGl2Pg==

Sorry but I've been searching everywhere for a solution and I haven't found anything, so I have no idea on how to solve this.

I have an encoded base64 which I need to place inside a html file and then decode it when the page loads. I've seen before that it is possible, but I can't remember how to do that.

This is my encoded code:

PGRpdiBpZD0idGhlZGl2Ij4NCjxpbWcgc3JjPSJodHRwczovL21lZGlhLmdpcGh5LmNvbS9tZWRpYS9oTTg3RE1ubHM1b1p5L2dpcGh5LmdpZiIvPg0KPGEgcmVsPSJodHRwOi8vZ29vZ2xlLmNvbSI+R29vZ2xlIElOQzwvYT48YnIgLz4NCjwhLS08aW1nIHNyYz0iaHR0cHM6Ly9tZWRpYS5naXBoeS5jb20vbWVkaWEvaE04N0RNbmxzNW9aeS9naXBoeS5naWYiLz4tLT4NCjwvZGl2Pg==
Share Improve this question asked Apr 20, 2018 at 0:14 user9459537user9459537 1451 gold badge3 silver badges9 bronze badges 7
  • 1 atob works in most browsers – Jaromanda X Commented Apr 20, 2018 at 0:15
  • why not just embed the giphy directly? – dandavis Commented Apr 20, 2018 at 0:19
  • because I need to encode the HTML too. Also, that code is just a sample. – user9459537 Commented Apr 20, 2018 at 0:20
  • Do you really mean in HTML or do you mean in JavaScript? – Joshua R. Commented Apr 20, 2018 at 0:21
  • Possible duplicate of Base64 encoding and decoding in client-side Javascript – Joshua R. Commented Apr 20, 2018 at 0:21
 |  Show 2 more ments

1 Answer 1

Reset to default 5

You can use javascript: atob()

var decodedHTML = window.atob('PGRpdiBpZD0idGhlZGl2Ij4NCjxpbWcgc3JjPSJodHRwczovL21lZGlhLmdpcGh5LmNvbS9tZWRpYS9oTTg3RE1ubHM1b1p5L2dpcGh5LmdpZiIvPg0KPGEgcmVsPSJodHRwOi8vZ29vZ2xlLmNvbSI+R29vZ2xlIElOQzwvYT48YnIgLz4NCjwhLS08aW1nIHNyYz0iaHR0cHM6Ly9tZWRpYS5naXBoeS5jb20vbWVkaWEvaE04N0RNbmxzNW9aeS9naXBoeS5naWYiLz4tLT4NCjwvZGl2Pg==');

If you console.log out decodedHTML you will see your html, which you can then place into an element using javascript.

Ex: document.querySelector('#someDiv').innerHTML = decodedHTML;

发布评论

评论列表(0)

  1. 暂无评论