This article describes the JavaScript implementation of page traversal callback method (involving Window.frames, recursive functions, function context). Share to everyone for your reference. as follows:
The pure JavaScript tool, which is distilled into my handwriting, is used to traverse all the pages of the current page and perform an iterative callback, and the return value of the callback function can be used for the result postback, which helps to reduce the closure variables ~
It is characterized in that the recursive traversal only retrieves the Window object of the child page, does not immediately execute the callback function, but recalls it in the normal loop structure after the retrieval has ended. This minimizes the memory consumption of recursive calls, simplifies the program structure, and makes it easy to maintain
Global function Frame_each (CallBack):
(function (BOM) {
function all_frames (iwindow) {
var _frames_ = [].slice.call (iwindow.frames, 0);
for (var i = 0; i < _frames_.length i++)
_frames_ = _frames_.concat (Arguments.callee (_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 < frames.length; i++) {
try {frames[i].name;} catch (Ierror) {continue;}
CBR = Callback.apply (Frames[i], [].slice.call (arguments, 1));
if (CBR = = false) break;
else if (CBR = = undefined) continue;
return CBR;
}}
;
(self);
Use examples:
No arguments--Returns an array containing the window object of the function call and the windows of its child pages, in the order of recursively traversing the
var Pages = Frame_each ();
Console.log (pages.length);
Define callback--Callback return value function corresponds to normal loop statement:
// 1. Undefined:continue
// 2 false:break
// 3. Any other value: Break && return Value
var search_result = Frame_each (function () {
var ifocus = this.document.activeElement;
switch (iFocus.tagName.toLowerCase ()) {case
' body ': return false;
Case ' iframe ': return ;
}
return ifocus;
});
search_result.innerhtml = ' Hello, focus! ';
I hope this article will help you with your JavaScript programming.