Main and IFrame communicate with each other class
Previously wrote a "IFrame and the main frame cross-domain mutual access method", introduced the main and IFRAME communication principle, do not understand the principle can first look.
Today, the method of communication between main and IFrame is packaged into classes, with two files,
JS:framemessage.js implements the interface that invokes the method, such as creating a temporary iframe across domains, calling the same domain performer.
PHP:FrameMessage.class.php implements the JS code that returns the execution method according to the parameter when the cross-domain request is received.
The functions are as follows:
1. Support for same domain and cross-domain communication
2. The passed method parameters support strings, JSON, arrays, and so on.
Framemessage.exec (' http://127.0.0.1/execB.php ', ' myframe ', ' fiframe ', [' fdipzone ', ' {' Gender ': ' Male ', ' age ': ' 29 '} ', ' ["Http://blog.csdn.net/fdipzone", "Http://weibo.com/fdipzone"]]);
Framemessage.exec (' http://localhost/execA.php ', ' ', ' fmain ', [' Programmer ', ' {' First ': ' php ', ' second ': ' JavaScript '} ', ' [' EEG ', ' nmg '] ']);
Because some browsers do not support json.stringify and json.parse methods (such as IE6/7), in order to be compatible, it is necessary to include json2.js, download address: http://www.php.cn/
Framemessage.js
/** Main and Iframe communication classes support the same domain and cross-domain communication *date:2013-12-29* author:fdipzone* ver:1.0*/var Framemessage = (function () { This.oframemessageexec = null; Temporary IFRAME/* Execute Method executor the page is executed, empty is the frame name of the method to be called by the same domain frame, or null is the method name that is called by the parent Func. Args the method to invoke parameter, must be an array [Arg1, arg2, Arg3, argn ...], convenient to apply the calling element to string format, do not use HTML, consider the problem of injection security will filter */this.exec = function (ex Ecutor, Frame, func, args) {this.executor = typeof (executor)! = ' undefined '? Executor: '; This.frame = typeof (frame)! = ' undefined '? Frame: "; This.func = typeof (func)! = ' undefined '? Func: "; This.args = typeof (args)! = ' undefined '? (__fisarray (args)? args: []): []; Must be an array if (executor== ') {__fsamedomainexec ();//same domain}else{__fcrossdomainex EC (); Cross domain}/* Same domain Execute */function __fsamedomainexec () {if (this.frame== ') {//Parent Parent.window[this.func].apply (this, This.args); }else{window.frames[this.frame][this.func].apply (this, This.args); }}/* cross-domain execution */function __fcrossdomainexec () {if (this.oframemessageexec = = null) {This.oframeme Ssageexec = document.createelement (' iframe '); This.oFrameMessageExec.name = ' framemessage_tmp_frame '; THIS.OFRAMEMESSAGEEXEC.SRC = __fgetsrc (); This.oFrameMessageExec.style.display = ' None '; Document.body.appendChild (this.oframemessageexec); }else{this.oFrameMessageExec.src = __fgetsrc (); }/* Gets the executed URL */function __fgetsrc () {return this.executor + (this.executor.indexOf ('? ') ==-1? '? ': ' & ') + ' frame= ' + this.frame + ' &func= ' + this.func + ' &args= ' + json.stringify (This.args) + ' &frame Message_rand= ' + math.random (); }/* Determines if the array */function __fisarray (obj) {return Object.prototype.toString.call (obj) = = = ' [Object Array] '; } return this; ());
FrameMessage.class.php
<?php/** Frame Message class Main and IFRAME communication classes * date:2013-12-29* author:fdipzone* ver:1.0** func:* Public execute calls the method according to the parameter * Private RETURNJS creates the returned javascript* private Jsformat escape parameter */class framemessage{//class start /* Execute according to the parameter call method * @param string $frame The frame name of the method to be called, or the parent * @param string $func The method name to invoke * @param Jsonstr $args parameter of the method to invoke * @return String */public static function execute ($frame, $func, $args = ') {if (!is_string ($frame) | |!is_string ($FUNC) | |!is_string ($ARGS)) {return '; }//frame with Func limit can only be alphanumeric underscore if ($frame! = ' &&!preg_match ('/^[a-za-z0-9_]+$/', $frame)) | |!preg_m Atch ('/^[a-za-z0-9_]+$/', $func)) {return '; } $params _str = "; if ($args) {$params = Json_decode ($args, true); if (Is_array ($params)) {for ($i =0, $len =count ($params); $i < $len; $i + +) {//filter parameters to prevent injection$params [$i] = Self::jsformat ($params [$i]); } $params _str = "'". Implode ("', '", $params). "'"; }} if ($frame = = ") {//Parent return Self::returnjs (" Parent.parent. "). $func. " (". $params _str.");} else{return Self::returnjs ("Parent.window."). $frame. ".". $func. " (". $params _str.");}} /** Create the returned JavaScript * @param string $STR * @return String */private static function Returnjs ($STR) { $ret = ' <script type= ' text/javascript > '. ' \ r \ n "; $ret. = $str. " \ r \ n "; $ret. = ' </script> '; return $ret; The/** escape parameter * @param string $STR * @return String */private static function Jsformat ($STR) {$str = Strip_tags (Trim ($STR)); Filter HTML $STR = str_replace (' \\s\\s ', ' \\s ', $str); $STR = Str_replace (CHR), ', $str); $STR = Str_replace (CHR), ", $STR); $str = Str_replace (', ', ', $str); $str = str_replace (' \ \ ', ' \\\\ ', $str); $str = Str_replace (' "', ' \ \" ', $str); $str = Str_replace (' \\\ ', ' \\\\\ ', $str); $str = Str_replace ("'", "\ '", $str); return $str; }}//Class end?>
A.html
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >B.html
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Execa.php and execb.php
<?phprequire ' FrameMessage.class.php '; $frame = isset ($_get[' frame ')? $_get[' frame ': '; $func = Isset ($_get[' func '])? $_get[' func ']: '; $args = isset ($_get[' args '])? $_get[' args ']: '; $result = Framemessage::execute ($frame, $func, $args); Echo $result; >
The above is the PHP main and IFRAME Mutual communication class (same domain/cross-domain) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!