JavaScript implements the method of Web page traversal callback (involving Window.frames, recursive functions, function contexts) _javascript techniques

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.