Iframe source with variable

老子叫甜甜 提交于 2019-12-12 16:09:50

问题


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?


回答1:


The Data URI scheme is for direct embedded data.

data:text/html;base64,aHR0cDovL2V4YW1wbGUuY29t

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

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




回答2:


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



回答3:


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

Live demo: http://jsfiddle.net/Ft9gh/

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


来源:https://stackoverflow.com/questions/13958097/iframe-source-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!