2

A script, for example, can either be linked:

<script src="myscript.js"></script>

or embedded:

<script type="text/javascript">
    //myscript
</script>

Is there a way to do this with iframes? Manually embed a webpage within a webpage before reaching the client's browser?

To the same effect, is it similarly possible to embed the decoded base64 of an image between img tags?

House3272
  • 1,007
  • 5
  • 14
  • 29
  • 1
    http://stackoverflow.com/questions/8240101/set-content-of-iframe might do the trick for iframes. Images support data URIs. https://css-tricks.com/data-uris/ – ceejayoz Apr 25 '16 at 00:42
  • 1
    You can also use `srcdoc` attribute if you don't care about older browser compatibility: http://www.w3schools.com/tags/att_iframe_srcdoc.asp – Selcuk Apr 25 '16 at 00:43

3 Answers3

0

You're looking for the srcdoc attribute. It's got pretty good support, but no IE/Edge yet.

http://caniuse.com/#feat=iframe-srcdoc

odnamrataizem
  • 231
  • 2
  • 7
0

The fact that IE/Edge doesn't support srcdoc is really annoying but anyway you can use JavaScript to do that and it works in all browsers including Internet explorer and Edge :

var content = '<html><body> <h1>My Sample HTML Code</h1></body></html>';
var iframeDoc = document.querySelector('#myIframeId').contentWindow.document;
iframeDoc.open('text/html', 'replace');
iframeDoc.write(content);
iframeDoc.close();
  <iframe id="myIframeId" />
akardon
  • 43,164
  • 4
  • 34
  • 42
0

Actually I've used it in conjunction with AngularJS. I mean Instead of :

<iframe srcdoc="{{htmlContent}}" />

I did :

$scope.$watch('htmlContent', function () {
            var iframeDoc = document.querySelector('iframe#preview').contentWindow.document;
            var content = $scope.htmlContent;
            iframeDoc.open('text/html', 'replace');
            iframeDoc.write(content);
            iframeDoc.close();
        });
<iframe id="preview" />

So that it works on all browsers (tested on Chrome,Firefox,IE11,Edge)

akardon
  • 43,164
  • 4
  • 34
  • 42