简体   繁体   中英

how can i import an iframe html code from a string

<iframe width="560" height="315" allow="fullscreen; autoplay; encrypted-media" src="https://games.construct.net/174/latest" frameborder="0" allowfullscreen="true" msallowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" allowpaymentrequest="false" referrerpolicy="unsafe-url" sandbox="allow-same-origin allow-forms allow-scripts allow-pointer-lock allow-orientation-lock allow-popups" scrolling="no"></iframe>

I have this source code as a string . how can i change it into an HTML code to run? I tried appendChiled command but i don't know how i should use it.

I just got errors or empty iframes that show this string code on it.

I made a snippet for your below. I'm not completely sure what you're looking for based on your question but let me know if this works for you.

 //Set string as a variable var string = '<iframe width="560" height="315" allow="fullscreen; autoplay; encrypted-media" src="https://games.construct.net/174/latest" frameborder="0" allowfullscreen="true" msallowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" allowpaymentrequest="false" referrerpolicy="unsafe-url" sandbox="allow-same-origin allow-forms allow-scripts allow-pointer-lock allow-orientation-lock allow-popups" scrolling="no"></iframe>' //Get container div by its id var container = document.getElementById("container") //Check if container has content, if there's nothing in the container then show the string if (container.innerHTML.length < 1) { container.innerHTML = string; }
 <div id="container"></div>

you can pass the string inside html() function using jquery

Example :

var x = "

hello world

"

$("#div_id").html(x);

in your case

var string = 'your iframe code as string'

$("#container").html(string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM