gpt4 book ai didi

javascript - 检测页面中的所有iframe是否都有src

转载 作者:行者123 更新时间:2023-11-30 05:54:34 27 4
gpt4 key购买 nike

我有一个包含动态数量的 iframe 的页面。 window.print() 必须在 iframe 加载后调用(即具有 src)。

如何在不迂腐的情况下用纯 JavaScript 优雅地处理这个问题?

最佳答案

function getLoadedFrames () {

var frames = document.getElementsByTagName( 'iframe' ), // Get all iframes
loadedFrames = [], i;

for ( i = 0; i < frames.length; i++ ) {
/*
If iframe has a src attribute, that attribute has a value AND
that value is not equal to the current window location (leaving src
blank seems to return the current location instead of empty string)
then add the frame to the loadedFrames array.
*/
if ( frames[ i ].src && frames[ i ].src.length > 0 && frames[ i ].src !== window.location.href ) {
loadedFrames.push( frames[ i ] );
}
}

return loadedFrames; // An array of your 'loaded' frames
}

关于javascript - 检测页面中的所有iframe是否都有src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com