如果iframe内容为空,则返回PHP变量

echo PHP variable if iframe content is empty

本文关键字:返回 PHP 变量 iframe 如果      更新时间:2023-09-26

我有这个JS/PHP代码:

<iframe src="" id="contacts_iframe" width="100%" height="300px" scrolling="no" frameborder="0"></iframe>
<script>
function() {
    $('#contacts_iframe').load(function() {            
        if($.trim($(this).contents().find("body").html()) == "") {
            alert("empty");
            <?php echo $ContactsDisplay; ?>
        }
    });
});
</script>

如果iframe为空,我希望能够回显$ContactsDisplay变量

我在中添加了警报进行测试,但警报没有显示,PHP变量也没有显示。

可能出了什么问题?

你几乎做到了。

 if($( "#frameDemo" ).contents().length == 0){
   alert('success loaded');
 } else {
   alert('fail not loaded');
 }
success
<iframe src="//api.jquery.com/" width="80%" height="600" id="frameDemo"></iframe>
fail
<iframe src="www.sdfsdfdsf.com" width="80%" height="600" id="frameDemo"></iframe>