Detect if the iframe content has loaded successfully

前端 未结 6 954
天命终不由人
天命终不由人 2020-11-27 19:11

I have a widget that contains an iframe. The user can configure the url of this iframe, but if the url could not be loaded (it does not exists or the user does not have acce

6条回答
  •  悲哀的现实
    2020-11-27 19:18

    How about checking if the url is available and only then setting the actual url of the iframe? e.g. with JQuery

    var url = "google.com"
    var loading_url = "/empty.html"
    document.getElementById("iframe").src = loading_url;
    $.ajax({
        url: url,
        type: 'GET',
        complete: function(e, xhr, settings){
             if(e.status === 200){
                  document.getElementById("iframe").src = url;
             }
        }
    });
    

    Edit: This does not seem to work cross domain, the status code is 0 in those cases.

提交回复
热议问题