- Поместив html внутри iframe (используя javascript)
- How to Load HTML, CSS, and JS Code into an iFrame
- Attempt #1: Using srcdoc
- 1. Browser Support for srcdoc is not great
- 2. It’s possible to «escape» from CSS/JS
- Attempt #2: Serverless Boomerang
- Attempt #3: Serverless Boomerang (redux)
- Solution: Blob URLs
- Blobs
- The Moral?
- Manipulate iFrame Content
- Demo
- Your Site’s Code
- iFrame Site’s Code
Поместив html внутри iframe (используя javascript)
Можно ли создать пустой iframe в качестве заполнителя, чтобы впоследствии вставить html в него?
В других словах предположим, что у меня есть пустой iframe с id,
Как мне вставить html в него?
Я использую jquery, если это упростит.
Вы можете сделать это без jQuery также:
var iframe = document.getElementById('iframeID'); iframe = iframe.contentWindow || ( iframe.contentDocument.document || iframe.contentDocument); iframe.document.open(); iframe.document.write('Hello World!'); iframe.document.close();
jQuery html разделяет тело, html и head теги из вставленного HTML.
Посмотрите на исходной странице этой страницы: http://mg.to/test/dynaframe.html Кажется, он делает именно то, что вы хотите сделать.
Нет, нет. Вы изменили бы script следующим образом:
iframeElementContainer = document.getElementById('BOX_IFRAME').contentDocument; iframeElementContainer.open(); iframeElementContainer.writeln("hello"); iframeElementContainer.close();
Да, я знаю, что это возможно, но главная проблема заключается в том, что мы не можем обрабатывать фрейм извне. Я уже загрузил какую-то другую вещь.
Тогда его невозможно ударить.
У меня та же проблема, где я должен показать фрагмент HTML-кода в iframe динамически из базы данных без атрибута SRC iframe, и я исправил ту же проблему с помощью следующего кода
Я надеюсь, что это поможет кому-то, кто имеет такое же требование, как я.
How to Load HTML, CSS, and JS Code into an iFrame
If you’re just here for the answer, not the story, the solution is at the bottom. If you’ve ever used JSFiddle, Codepen, or others, this problem will be familiar to you: The goal is to take some HTML, CSS, and JS (stored as strings) and create an iframe with the code loaded inside. This problem should be easy, but it isn’t. At least. It wasn’t, until I found the golden ticket I had been waiting for all along. But more on that later. Let’s start with all the things that didn’t work, because that’s more fun.
Attempt #1: Using srcdoc
After doing a bit of research, I was thrilled to discover that it’s possible to add a srcdoc attribute to iframes. If you pass in an HTML string, the iframe will load with that HTML content inside:
srcdoc="This text will appear in the iframe!
">
1. Browser Support for srcdoc is not great
If we want to support IE or Edge, we’ll need a different approach (or a polyfill).
2. It’s possible to «escape» from CSS/JS
function setIframeContent(iframe, html, css, js >) const source = ` $css> $html> $js>