Elm: attribute “onerror” adds “data-onerror” attribute instead












8














In Elm I have a simple image, and I want it to be replaced by some 'missing' image onerror. So I added an "onerror" attribute:



img
[ src "broken-link.png"
, attribute "onerror" "this.onerror=null;this.src='missing.png';"
]


However, when I look at the generated html, the img doesn't get an onerror attribute, but rather gets an data-onerror, and ofcourse this doesn't work.



Why is this? And how do I fix it?



Here is a little example I made with my friend Bulbasaur to illustrate the problem: https://ellie-app.com/3Yn8Y6Rmvrqa1










share|improve this question



























    8














    In Elm I have a simple image, and I want it to be replaced by some 'missing' image onerror. So I added an "onerror" attribute:



    img
    [ src "broken-link.png"
    , attribute "onerror" "this.onerror=null;this.src='missing.png';"
    ]


    However, when I look at the generated html, the img doesn't get an onerror attribute, but rather gets an data-onerror, and ofcourse this doesn't work.



    Why is this? And how do I fix it?



    Here is a little example I made with my friend Bulbasaur to illustrate the problem: https://ellie-app.com/3Yn8Y6Rmvrqa1










    share|improve this question

























      8












      8








      8







      In Elm I have a simple image, and I want it to be replaced by some 'missing' image onerror. So I added an "onerror" attribute:



      img
      [ src "broken-link.png"
      , attribute "onerror" "this.onerror=null;this.src='missing.png';"
      ]


      However, when I look at the generated html, the img doesn't get an onerror attribute, but rather gets an data-onerror, and ofcourse this doesn't work.



      Why is this? And how do I fix it?



      Here is a little example I made with my friend Bulbasaur to illustrate the problem: https://ellie-app.com/3Yn8Y6Rmvrqa1










      share|improve this question













      In Elm I have a simple image, and I want it to be replaced by some 'missing' image onerror. So I added an "onerror" attribute:



      img
      [ src "broken-link.png"
      , attribute "onerror" "this.onerror=null;this.src='missing.png';"
      ]


      However, when I look at the generated html, the img doesn't get an onerror attribute, but rather gets an data-onerror, and ofcourse this doesn't work.



      Why is this? And how do I fix it?



      Here is a little example I made with my friend Bulbasaur to illustrate the problem: https://ellie-app.com/3Yn8Y6Rmvrqa1







      elm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 at 9:20









      The Oddler

      2,65022463




      2,65022463
























          2 Answers
          2






          active

          oldest

          votes


















          12














          Why is this?



          This seems to be a built-in undocumented safety feature of Elm.



          Checking source code of Elm, Html.attribute is defined as (source)



          attribute : String -> String -> Attribute msg
          attribute =
          VirtualDom.attribute


          and VirtualDom.attribute is defined as (source):



          attribute : String -> String -> Attribute msg
          attribute key value =
          Elm.Kernel.VirtualDom.attribute
          (Elm.Kernel.VirtualDom.noOnOrFormAction key)
          (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri value)


          Your attribute name onclick is passed to Elm.Kernel.VirtualDom.noOnOrFormAction which is defined in JavaScript as (source):



          function _VirtualDom_noOnOrFormAction(key)
          {
          return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
          }


          So if attribute name starts with on, or is string formAction, then it is renamed to be a data-attribute.



          How do I fix it?



          One way I know how to fix this is to write the code in Elm without JavaScript. Here's full working example with main parts copied below: (This is based on accepted answer here about detecting image load failure.)



          1) Keep current URL in model



          type alias Model =
          { src : String
          }

          init : Model
          init =
          { src = "http://example.com" }


          2) Change event handler to send Elm message on image load error



          img
          [ src model.src
          , on "error" (Json.Decode.succeed ImageError)
          , alt "Should be Bulbasaur"
          ]


          3) Change URL in update on error



          update : Msg -> Model -> Model
          update msg model =
          case msg of
          ImageError ->
          { model
          | src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
          }





          share|improve this answer





























            7














            I'd solve this problem by writing a custom event handler for image load error event. The custom handler will be look a like:



            onImageLoadError : msg -> Html.Attribute msg
            onImageLoadError message =
            on "error" (JD.succeed message)


            I'll define the Model, Msg as below:



            type Msg
            = ImageLoadError


            type alias Model =
            ImageSrc


            I'll also define a custom type to wrap the good and default url.



            type ImageSrc
            = Good String
            | Default String


            The onImageLoadError will be fired and send the ImageLoadError msg to the update function, and the update function will then set the default url as:



            update : Msg -> Model -> Model
            update msg model =
            case msg of
            ImageLoadError ->
            Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


            And here is the full runnable code:



            module Main exposing (main)

            import Browser
            import Html exposing (Html, div, img, text)
            import Html.Attributes exposing (alt, src)
            import Html.Events exposing (on)
            import Json.Decode as JD


            type Msg
            = ImageLoadError


            type ImageSrc
            = Good String
            | Default String


            type alias Model =
            ImageSrc


            update : Msg -> Model -> Model
            update msg model =
            case msg of
            ImageLoadError ->
            Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


            imageSrcVal : ImageSrc -> String
            imageSrcVal src =
            case src of
            Good url ->
            url

            Default url ->
            url


            view : Model -> Html Msg
            view model =
            div
            [ div [ text "Broken image, that should be replaced by Bulbasaur: " ]
            , img
            [ src (imageSrcVal model)
            , alt "Should be Bulbasaur"
            , onImageLoadError ImageLoadError
            ]

            , div [ text "instead it does nothing, and the 'onerror' attribute is 'data-onerror' (use inspect element to see)" ]
            ]


            onImageLoadError : msg -> Html.Attribute msg
            onImageLoadError message =
            on "error" (JD.succeed message)


            main : Program () Model Msg
            main =
            Browser.sandbox
            { init = Good "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg"
            , view = view
            , update = update
            }


            Ellie App.



            I init the model with a good url "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", but if you somehow make it a bad url like "https://bad.developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", you will the default image there.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456798%2felm-attribute-onerror-adds-data-onerror-attribute-instead%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              12














              Why is this?



              This seems to be a built-in undocumented safety feature of Elm.



              Checking source code of Elm, Html.attribute is defined as (source)



              attribute : String -> String -> Attribute msg
              attribute =
              VirtualDom.attribute


              and VirtualDom.attribute is defined as (source):



              attribute : String -> String -> Attribute msg
              attribute key value =
              Elm.Kernel.VirtualDom.attribute
              (Elm.Kernel.VirtualDom.noOnOrFormAction key)
              (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri value)


              Your attribute name onclick is passed to Elm.Kernel.VirtualDom.noOnOrFormAction which is defined in JavaScript as (source):



              function _VirtualDom_noOnOrFormAction(key)
              {
              return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
              }


              So if attribute name starts with on, or is string formAction, then it is renamed to be a data-attribute.



              How do I fix it?



              One way I know how to fix this is to write the code in Elm without JavaScript. Here's full working example with main parts copied below: (This is based on accepted answer here about detecting image load failure.)



              1) Keep current URL in model



              type alias Model =
              { src : String
              }

              init : Model
              init =
              { src = "http://example.com" }


              2) Change event handler to send Elm message on image load error



              img
              [ src model.src
              , on "error" (Json.Decode.succeed ImageError)
              , alt "Should be Bulbasaur"
              ]


              3) Change URL in update on error



              update : Msg -> Model -> Model
              update msg model =
              case msg of
              ImageError ->
              { model
              | src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
              }





              share|improve this answer


























                12














                Why is this?



                This seems to be a built-in undocumented safety feature of Elm.



                Checking source code of Elm, Html.attribute is defined as (source)



                attribute : String -> String -> Attribute msg
                attribute =
                VirtualDom.attribute


                and VirtualDom.attribute is defined as (source):



                attribute : String -> String -> Attribute msg
                attribute key value =
                Elm.Kernel.VirtualDom.attribute
                (Elm.Kernel.VirtualDom.noOnOrFormAction key)
                (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri value)


                Your attribute name onclick is passed to Elm.Kernel.VirtualDom.noOnOrFormAction which is defined in JavaScript as (source):



                function _VirtualDom_noOnOrFormAction(key)
                {
                return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
                }


                So if attribute name starts with on, or is string formAction, then it is renamed to be a data-attribute.



                How do I fix it?



                One way I know how to fix this is to write the code in Elm without JavaScript. Here's full working example with main parts copied below: (This is based on accepted answer here about detecting image load failure.)



                1) Keep current URL in model



                type alias Model =
                { src : String
                }

                init : Model
                init =
                { src = "http://example.com" }


                2) Change event handler to send Elm message on image load error



                img
                [ src model.src
                , on "error" (Json.Decode.succeed ImageError)
                , alt "Should be Bulbasaur"
                ]


                3) Change URL in update on error



                update : Msg -> Model -> Model
                update msg model =
                case msg of
                ImageError ->
                { model
                | src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
                }





                share|improve this answer
























                  12












                  12








                  12






                  Why is this?



                  This seems to be a built-in undocumented safety feature of Elm.



                  Checking source code of Elm, Html.attribute is defined as (source)



                  attribute : String -> String -> Attribute msg
                  attribute =
                  VirtualDom.attribute


                  and VirtualDom.attribute is defined as (source):



                  attribute : String -> String -> Attribute msg
                  attribute key value =
                  Elm.Kernel.VirtualDom.attribute
                  (Elm.Kernel.VirtualDom.noOnOrFormAction key)
                  (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri value)


                  Your attribute name onclick is passed to Elm.Kernel.VirtualDom.noOnOrFormAction which is defined in JavaScript as (source):



                  function _VirtualDom_noOnOrFormAction(key)
                  {
                  return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
                  }


                  So if attribute name starts with on, or is string formAction, then it is renamed to be a data-attribute.



                  How do I fix it?



                  One way I know how to fix this is to write the code in Elm without JavaScript. Here's full working example with main parts copied below: (This is based on accepted answer here about detecting image load failure.)



                  1) Keep current URL in model



                  type alias Model =
                  { src : String
                  }

                  init : Model
                  init =
                  { src = "http://example.com" }


                  2) Change event handler to send Elm message on image load error



                  img
                  [ src model.src
                  , on "error" (Json.Decode.succeed ImageError)
                  , alt "Should be Bulbasaur"
                  ]


                  3) Change URL in update on error



                  update : Msg -> Model -> Model
                  update msg model =
                  case msg of
                  ImageError ->
                  { model
                  | src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
                  }





                  share|improve this answer












                  Why is this?



                  This seems to be a built-in undocumented safety feature of Elm.



                  Checking source code of Elm, Html.attribute is defined as (source)



                  attribute : String -> String -> Attribute msg
                  attribute =
                  VirtualDom.attribute


                  and VirtualDom.attribute is defined as (source):



                  attribute : String -> String -> Attribute msg
                  attribute key value =
                  Elm.Kernel.VirtualDom.attribute
                  (Elm.Kernel.VirtualDom.noOnOrFormAction key)
                  (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri value)


                  Your attribute name onclick is passed to Elm.Kernel.VirtualDom.noOnOrFormAction which is defined in JavaScript as (source):



                  function _VirtualDom_noOnOrFormAction(key)
                  {
                  return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
                  }


                  So if attribute name starts with on, or is string formAction, then it is renamed to be a data-attribute.



                  How do I fix it?



                  One way I know how to fix this is to write the code in Elm without JavaScript. Here's full working example with main parts copied below: (This is based on accepted answer here about detecting image load failure.)



                  1) Keep current URL in model



                  type alias Model =
                  { src : String
                  }

                  init : Model
                  init =
                  { src = "http://example.com" }


                  2) Change event handler to send Elm message on image load error



                  img
                  [ src model.src
                  , on "error" (Json.Decode.succeed ImageError)
                  , alt "Should be Bulbasaur"
                  ]


                  3) Change URL in update on error



                  update : Msg -> Model -> Model
                  update msg model =
                  case msg of
                  ImageError ->
                  { model
                  | src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 at 10:06









                  Markus Laire

                  1,8371120




                  1,8371120

























                      7














                      I'd solve this problem by writing a custom event handler for image load error event. The custom handler will be look a like:



                      onImageLoadError : msg -> Html.Attribute msg
                      onImageLoadError message =
                      on "error" (JD.succeed message)


                      I'll define the Model, Msg as below:



                      type Msg
                      = ImageLoadError


                      type alias Model =
                      ImageSrc


                      I'll also define a custom type to wrap the good and default url.



                      type ImageSrc
                      = Good String
                      | Default String


                      The onImageLoadError will be fired and send the ImageLoadError msg to the update function, and the update function will then set the default url as:



                      update : Msg -> Model -> Model
                      update msg model =
                      case msg of
                      ImageLoadError ->
                      Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                      And here is the full runnable code:



                      module Main exposing (main)

                      import Browser
                      import Html exposing (Html, div, img, text)
                      import Html.Attributes exposing (alt, src)
                      import Html.Events exposing (on)
                      import Json.Decode as JD


                      type Msg
                      = ImageLoadError


                      type ImageSrc
                      = Good String
                      | Default String


                      type alias Model =
                      ImageSrc


                      update : Msg -> Model -> Model
                      update msg model =
                      case msg of
                      ImageLoadError ->
                      Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                      imageSrcVal : ImageSrc -> String
                      imageSrcVal src =
                      case src of
                      Good url ->
                      url

                      Default url ->
                      url


                      view : Model -> Html Msg
                      view model =
                      div
                      [ div [ text "Broken image, that should be replaced by Bulbasaur: " ]
                      , img
                      [ src (imageSrcVal model)
                      , alt "Should be Bulbasaur"
                      , onImageLoadError ImageLoadError
                      ]

                      , div [ text "instead it does nothing, and the 'onerror' attribute is 'data-onerror' (use inspect element to see)" ]
                      ]


                      onImageLoadError : msg -> Html.Attribute msg
                      onImageLoadError message =
                      on "error" (JD.succeed message)


                      main : Program () Model Msg
                      main =
                      Browser.sandbox
                      { init = Good "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg"
                      , view = view
                      , update = update
                      }


                      Ellie App.



                      I init the model with a good url "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", but if you somehow make it a bad url like "https://bad.developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", you will the default image there.






                      share|improve this answer




























                        7














                        I'd solve this problem by writing a custom event handler for image load error event. The custom handler will be look a like:



                        onImageLoadError : msg -> Html.Attribute msg
                        onImageLoadError message =
                        on "error" (JD.succeed message)


                        I'll define the Model, Msg as below:



                        type Msg
                        = ImageLoadError


                        type alias Model =
                        ImageSrc


                        I'll also define a custom type to wrap the good and default url.



                        type ImageSrc
                        = Good String
                        | Default String


                        The onImageLoadError will be fired and send the ImageLoadError msg to the update function, and the update function will then set the default url as:



                        update : Msg -> Model -> Model
                        update msg model =
                        case msg of
                        ImageLoadError ->
                        Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                        And here is the full runnable code:



                        module Main exposing (main)

                        import Browser
                        import Html exposing (Html, div, img, text)
                        import Html.Attributes exposing (alt, src)
                        import Html.Events exposing (on)
                        import Json.Decode as JD


                        type Msg
                        = ImageLoadError


                        type ImageSrc
                        = Good String
                        | Default String


                        type alias Model =
                        ImageSrc


                        update : Msg -> Model -> Model
                        update msg model =
                        case msg of
                        ImageLoadError ->
                        Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                        imageSrcVal : ImageSrc -> String
                        imageSrcVal src =
                        case src of
                        Good url ->
                        url

                        Default url ->
                        url


                        view : Model -> Html Msg
                        view model =
                        div
                        [ div [ text "Broken image, that should be replaced by Bulbasaur: " ]
                        , img
                        [ src (imageSrcVal model)
                        , alt "Should be Bulbasaur"
                        , onImageLoadError ImageLoadError
                        ]

                        , div [ text "instead it does nothing, and the 'onerror' attribute is 'data-onerror' (use inspect element to see)" ]
                        ]


                        onImageLoadError : msg -> Html.Attribute msg
                        onImageLoadError message =
                        on "error" (JD.succeed message)


                        main : Program () Model Msg
                        main =
                        Browser.sandbox
                        { init = Good "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg"
                        , view = view
                        , update = update
                        }


                        Ellie App.



                        I init the model with a good url "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", but if you somehow make it a bad url like "https://bad.developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", you will the default image there.






                        share|improve this answer


























                          7












                          7








                          7






                          I'd solve this problem by writing a custom event handler for image load error event. The custom handler will be look a like:



                          onImageLoadError : msg -> Html.Attribute msg
                          onImageLoadError message =
                          on "error" (JD.succeed message)


                          I'll define the Model, Msg as below:



                          type Msg
                          = ImageLoadError


                          type alias Model =
                          ImageSrc


                          I'll also define a custom type to wrap the good and default url.



                          type ImageSrc
                          = Good String
                          | Default String


                          The onImageLoadError will be fired and send the ImageLoadError msg to the update function, and the update function will then set the default url as:



                          update : Msg -> Model -> Model
                          update msg model =
                          case msg of
                          ImageLoadError ->
                          Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                          And here is the full runnable code:



                          module Main exposing (main)

                          import Browser
                          import Html exposing (Html, div, img, text)
                          import Html.Attributes exposing (alt, src)
                          import Html.Events exposing (on)
                          import Json.Decode as JD


                          type Msg
                          = ImageLoadError


                          type ImageSrc
                          = Good String
                          | Default String


                          type alias Model =
                          ImageSrc


                          update : Msg -> Model -> Model
                          update msg model =
                          case msg of
                          ImageLoadError ->
                          Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                          imageSrcVal : ImageSrc -> String
                          imageSrcVal src =
                          case src of
                          Good url ->
                          url

                          Default url ->
                          url


                          view : Model -> Html Msg
                          view model =
                          div
                          [ div [ text "Broken image, that should be replaced by Bulbasaur: " ]
                          , img
                          [ src (imageSrcVal model)
                          , alt "Should be Bulbasaur"
                          , onImageLoadError ImageLoadError
                          ]

                          , div [ text "instead it does nothing, and the 'onerror' attribute is 'data-onerror' (use inspect element to see)" ]
                          ]


                          onImageLoadError : msg -> Html.Attribute msg
                          onImageLoadError message =
                          on "error" (JD.succeed message)


                          main : Program () Model Msg
                          main =
                          Browser.sandbox
                          { init = Good "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg"
                          , view = view
                          , update = update
                          }


                          Ellie App.



                          I init the model with a good url "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", but if you somehow make it a bad url like "https://bad.developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", you will the default image there.






                          share|improve this answer














                          I'd solve this problem by writing a custom event handler for image load error event. The custom handler will be look a like:



                          onImageLoadError : msg -> Html.Attribute msg
                          onImageLoadError message =
                          on "error" (JD.succeed message)


                          I'll define the Model, Msg as below:



                          type Msg
                          = ImageLoadError


                          type alias Model =
                          ImageSrc


                          I'll also define a custom type to wrap the good and default url.



                          type ImageSrc
                          = Good String
                          | Default String


                          The onImageLoadError will be fired and send the ImageLoadError msg to the update function, and the update function will then set the default url as:



                          update : Msg -> Model -> Model
                          update msg model =
                          case msg of
                          ImageLoadError ->
                          Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                          And here is the full runnable code:



                          module Main exposing (main)

                          import Browser
                          import Html exposing (Html, div, img, text)
                          import Html.Attributes exposing (alt, src)
                          import Html.Events exposing (on)
                          import Json.Decode as JD


                          type Msg
                          = ImageLoadError


                          type ImageSrc
                          = Good String
                          | Default String


                          type alias Model =
                          ImageSrc


                          update : Msg -> Model -> Model
                          update msg model =
                          case msg of
                          ImageLoadError ->
                          Default "https://developer.mozilla.org/static/img/web-docs-sprite.22a6a085cf14.svg"


                          imageSrcVal : ImageSrc -> String
                          imageSrcVal src =
                          case src of
                          Good url ->
                          url

                          Default url ->
                          url


                          view : Model -> Html Msg
                          view model =
                          div
                          [ div [ text "Broken image, that should be replaced by Bulbasaur: " ]
                          , img
                          [ src (imageSrcVal model)
                          , alt "Should be Bulbasaur"
                          , onImageLoadError ImageLoadError
                          ]

                          , div [ text "instead it does nothing, and the 'onerror' attribute is 'data-onerror' (use inspect element to see)" ]
                          ]


                          onImageLoadError : msg -> Html.Attribute msg
                          onImageLoadError message =
                          on "error" (JD.succeed message)


                          main : Program () Model Msg
                          main =
                          Browser.sandbox
                          { init = Good "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg"
                          , view = view
                          , update = update
                          }


                          Ellie App.



                          I init the model with a good url "https://developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", but if you somehow make it a bad url like "https://bad.developer.mozilla.org/static/arrows/arrow-right.cbc8b4f075cc.svg", you will the default image there.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 24 at 10:17

























                          answered Nov 24 at 10:08









                          Arup Rakshit

                          96.3k23187235




                          96.3k23187235






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456798%2felm-attribute-onerror-adds-data-onerror-attribute-instead%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Ellipse (mathématiques)

                              Quarter-circle Tiles

                              Mont Emei