Elm: how to use an iframe

旧城冷巷雨未停 提交于 2019-12-23 15:39:25

问题


I want to render an iframe given a String input. For example, I have some string of HTML like this:

str = "<!DOCTYPE html><html><head></head><body>HELLO ELM</body></html>"

In JS I would do this by setting innerHtml on the iframe.

For now, I'm trying to just get the iframe to render anything at all, but haven't had success. I'm trying this sort of thing:

iframe 
    []
    [ body [] [ div [][text "hi"], div [][text "hi"] ] ] 

Once I get it working with a list of HTML nodes, I'll move onto making it work with a string. What I'm looking for would be some kind of stringToHtml: String -> List Html method.

Any help or suggestions for getting started with iframes in elm?


回答1:


You can use the srcdoc attribute of an iframe to set the html from an html string, like so:

import Html exposing (..)
import Html.Attributes exposing (srcdoc)

main =
  div [ ]
    [ div [] [ text "Outside the iframe" ]
    , iframe
      [ srcdoc "<div>Inside the iframe</div>" ]
      []
    ]

(IE and Edge do not currently support srcdoc but there is a polyfill)

In the end, though, that's a rather brittle solution because it bypasses the Virtual DOM. At this time, there appears no other way to manipulate iframe content within the Virtual DOM. Could your content instead be stored in another page and referenced through the iframe src attribute?



来源:https://stackoverflow.com/questions/34539606/elm-how-to-use-an-iframe

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