Javascript method for implementing page traversal callback (involving window. frames, recursive functions, and function context) _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to implement page traversal callback in javascript (involving window. frames, recursive functions, and function context), involving javascript callback, traversal, and other implementation techniques, for more information about how to implement page traversal callback using javascript, see the following example. frames, recursive functions, and function context ). Share it with you for your reference. The details are as follows:

Extracted from my hand-written JavaScript tool program, it is used to traverse all subpages of the current page and execute iterative callback. The callback function return value can be used for result return, which helps reduce closure variables ~

Its feature is that only Window objects on sub-pages are retrieved through recursive traversal. Instead of executing the callback function immediately, callback is performed in the general loop structure after the retrieval is complete. In this way, the memory consumption during recursive calls can be minimized, and the program structure is simplified and 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);

Example:

// No parameter -- returns an array containing the Window object where the function call is located and the Window of its subpage. The sequence is the same as recursively traversing var Pages = Frame_Each (); console. log (Pages. length); // define the callback -- the callback return value function corresponds to a general loop statement: // 1. undefined: continue // 2. false: break // 3. other values: break & return Valuevar Search_Result = Frame_Each (function () {var iFocus = this.doc ument. 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 design javascript programs.

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.