php main 与 iframe 相互通讯类(js+php同域/跨域)(2)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> main window </title> <script type="text/javascript" src="https://www.jb51.net/json2.js"></script> <script type="text/javascript" src="https://www.jb51.net/FrameMessage.js"></script> <script type="text/javascript"> // main js function function fMain(profession, skill, company){ var skill_p = JSON.parse(skill); var company_p = JSON.parse(company); var msg = "main function execute success\n\n"; msg += "profession:" + profession + "\n"; msg += "first skill:" + skill_p.first + "\n"; msg += "second skill:" + skill_p.second + "\n"; msg += "company1:" + company_p[0] + "\n"; msg += "company2:" + company_p[1] + "\n"; alert(msg); } // exec iframe function function exec_iframe(){ // same domain //FrameMessage.exec('', 'myframe', 'fIframe', ['fdipzone', '{"gender":"male","age":"29"}', '["http://blog.csdn.net/fdipzone", "http://weibo.com/fdipzone"]']); // cross domain 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"]']); } </script> </head> <body> <p>A.html main</p> <p><input type="button" value="exec iframe function"></p> <!-- same domain --> <!--<iframe src="https://www.jb51.net/article/B.html"></iframe>--> <!-- cross domain --> <iframe src="https://127.0.0.1/B.html"></iframe> </body> </html>

B.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> iframe window </title> <script type="text/javascript" src="https://www.jb51.net/json2.js"></script> <script type="text/javascript" src="https://www.jb51.net/FrameMessage.js"></script> <script type="text/javascript"> // iframe js function function fIframe(name, obj, arr){ var obj_p = JSON.parse(obj); var arr_p = JSON.parse(arr); var msg = "iframe function execute success\n\n"; msg += "name:" + name + "\n"; msg += "gender:" + obj_p.gender + "\n"; msg += "age:" + obj_p.age + "\n"; msg += "blog:" + arr_p[0] + "\n"; msg += "weibo:" + arr_p[1] + "\n"; alert(msg); } // exec main function function exec_main(){ // same domain //FrameMessage.exec('', '', 'fMain', ['programmer', '{"first":"PHP","second":"javascript"}', '["EEG","NMG"]']); // cross domain FrameMessage.exec('http://localhost/execA.php', '', 'fMain', ['programmer', '{"first":"PHP","second":"javascript"}', '["EEG","NMG"]']); } </script> </head> <body> <p>B.html iframe</p> <p><input type="button" value="exec main function"></p> </body> </html>

execA.php 与 execB.php

<?php require '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; ?>

源码下载地址:点击查看

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyzpzf.html