This article describes the method of implementing web page subpage traversal callbacks (involving , recursive functions, function contexts) in JavaScript. Share it for your reference. The details are as follows:
A pure JavaScript tool program refined from my handwritten handwritten is used to iterate through all subpages of the current web page and execute iterative callbacks. The return value of the callback function can be used for result return, which helps to reduce closure variables~
Its characteristic is that when recursive traversal, only the Window object of the subpage is retrieved, and the callback function is not executed immediately, but the callback function is called back in the normal loop structure after the search is completed. This can minimize memory consumption during recursive calls, simplify the program structure and make it easy to maintain
Global function Frame_Each( CallBack):
(function (BOM) { function All_Frames(iWindow) { var _Frames_ = [].(, 0); for (var i = 0; i < _Frames_.length; i++) _Frames_ = _Frames_.concat( (_Frames_[i]) ); return _Frames_; } BOM.Frame_Each = function (CallBack) { var Frames = [this].concat( All_Frames(this) ); if (! CallBack) return Frames; for (var i = 0, CBR; i < ; i++) { try { Frames[i].name; } catch (iError) { continue; } CBR = (Frames[i], [].(arguments, 1)); if (CBR === false) break; else if (CBR === undefined) continue; return CBR; } }; })(self);
Example of usage:
// No parameter - Return an array containing the Window object and its subpage where the function call is located, and its order is the same as recursive traversalvar Pages = Frame_Each(); ( ); // Define callback — The callback return value function corresponds to ordinary loop statements:// 1. undefined:continue // 2. false:break // 3. Any other value: break && return Valuevar Search_Result = Frame_Each(function () { var iFocus = ; switch ( () ) { case 'body': return false; case 'iframe': return; } return iFocus; }); Search_Result.innerHTML = 'Hello, Focus!';
I hope this article will be helpful to everyone's JavaScript programming.