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

javascript - Iframe source with variable - Stack Overflow

programmeradmin1浏览0评论

I have a couple of questions:

  1. Is <iframe src="/index.php"></iframe> the same as HTMLIFrameElement ?

  2. Why

    <iframe src="data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t"
        height=1280 width=800></iframe>
    

    works fine, but

    a=document.getElementsByTagName('body')[0];
    b=document.createElement('iframe');
    b.src="data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t";
    a.appendChild(b);
    

    does not work? I mean in DOM HTMLIFrameElement src I could put function with encoded string. Is it possible to encode string only in src variable?

I have a couple of questions:

  1. Is <iframe src="/index.php"></iframe> the same as HTMLIFrameElement ?

  2. Why

    <iframe src="data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t"
        height=1280 width=800></iframe>
    

    works fine, but

    a=document.getElementsByTagName('body')[0];
    b=document.createElement('iframe');
    b.src="data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t";
    a.appendChild(b);
    

    does not work? I mean in DOM HTMLIFrameElement src I could put function with encoded string. Is it possible to encode string only in src variable?

Share Improve this question edited Feb 6, 2015 at 1:18 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked Dec 19, 2012 at 17:45 AdamAdam 511 silver badge6 bronze badges 1
  • Just putting it here so everyone can see that the whole premise of your question is false. jsfiddle/JkJNH/2 <-- they work exactly the same, and that code is pretty much directly copypasted from your question. – Esailija Commented Dec 19, 2012 at 18:39
Add a ment  | 

3 Answers 3

Reset to default 3
  1. Yes
  2. You need to append the iFrame to the document. Like so document.body.appendChild(b)​

The Data URI scheme is for direct embedded data.

data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t

Means a html page with "http://example." as its sole content.

You probably want "http://example." as src

To your point 2: your two methods give the same result.

Live demo: http://jsfiddle/Ft9gh/

a=document.getElementsByTagName('body')[0];
b=document.createElement('iframe');
b.src="data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t";
a.appendChild(b); ​
发布评论

评论列表(0)

  1. 暂无评论