The previous blog post mentioned that a CG friend requested the implementation of various link opening effects on separate pages. It was mentioned that there is a problem with IE or other toolbar (yahoo, google) that can intercept pop-up windows and cannot be implemented in the user's browser. In addition to the method mentioned last time, which is to change the open method to ShowModelDialog method, CG today shares a method of forcing the user's browser to pop up windows using Flash/FLEX AS. CG feels that this method is a very rogue method because almost all user browsers have installed Flash PlugIn or ActiveX, and there is no way for browsers and toolbars to prevent pop-up windows using SWF files. Currently, most users, especially FF users, generally allow access to the network in terms of browser security.
Because AS has two versions, 2 and 3, CG here only specifically introduces the AS3 version. The implementation of AS2 is relatively simple. Everyone just needs to create a new AS2 flash file and then add the following code to the action:
getURL("http://www.demo.com");
Then generate the corresponding SWF file and simply publish it. This method is very simple, but what everyone needs to pay attention to is that because Flash needs to consider security issues, the URL of the pop-up window must be in the same domain as the URL of the pop-up window. For example, if http://www.demo.com/a.html is popped up, it can only be a page in the demo.com domain, otherwise a script error will be prompted.
This method is available in versions below IE6, but in IE7+ versions, please pay attention to setting the wmode of the flash playback control to window. This mode is a non-DOM-managed mode, which means it is independent of the container it is in.
Below, CG specifically explains how to implement it using the AS3 version. In AS3, the getURL method has been modified to the navigateToURL(url, target) method under the flash.net package. The first parameter is the URL, and the second is the opening method. If it is a pop-up window, it is "_blank". For the convenience of calling, CG wrote the following AS3 code, which uses the flash ExternalInterface interface to allow flash to implement pop-up windows more flexibly. For the convenience of debugging, CG uses the method of implementation on the client side, and everyone can clearly see the process of mutual calling between AS and JS. If everyone wants to put it on their own website, they can consider using the XML method to implement it, and the client will not be able to see the calling process. The code is omitted here.
In addition, everyone needs to pay attention to the security issues of Flash. Cross-domain access in AS requires client permission, as mentioned above, but there are more detailed settings under AS3.
The following is the AS3 code, using FLEX3 for debugging:
package {
import flash.display.Sprite;
import flash.external.ExternalInterface;
import flash.net.*;
public class IePopup extends Sprite
{
private var url ; //Define the URL to open
private var target; //Opening method
public function setUrl(str)
{
this.url = str;
}
public function getUrl()
{
return this.url;
}
public function setTarget(str)
{
this.target = str;
}
public function getTarget()
{
return this.target;
}
//constructor
public function IePopup()
{
this.register();
this.popup();
}
//register
private function register():void
{
//Listen for external calls, function name is jsCall, callback function name is jsCall
ExternalInterface.addCallback("jsCall",jsCall);
}
//popup
private function popup():void
{
var targetURL:URLRequest = new URLRequest(this.getUrl());
navigateToURL(targetURL,this.getTarget()); //Call to pop-up window
}
//jsCall
public function jsCall(url:String,tar:String = '\_blank'):void
{
if(url.length==0||tar=='')
return;
this.setUrl(url); //Set the string
if(tar.length==0||tar=='')
this.target=='\_blank'; //Set the default to new window
this.setTarget(tar);
this.popup();//Pop-up window
}
}
}
Code explanation: Two variables are defined to get and set the url and target parameters. When constructing the flash class, it will automatically register the function name and callback method for getting parameters. In the jsCall method, it gets the two parameters passed from JS, and after judging, it pops up the window.
The following is part of the JS code:
var ready=false;
//Flash activeX is ready?
function IsReady(){
return ready;
}
//Flash activeX ready
function Ready(){
ready=true;
}
//use Flash object
function domJsCall(url) {
window['IePopup'].jsCall(url); //for IE
//document['IePopup'].jsCall(url); //for others
}
//use XML
function xmlJsCall(url){
var xmlCall = "";
xmlCall += url;
xmlCall += "";
window['IePopup'].CallFunction(xmlCall);
}
function jsCall(url){
if(IsReady()){
//two methods
xmlJsCall(url); //XML
//domJsCall(url); //dom
}
}
Code explanation: Here, a variable is defined to check whether the Flash object is loaded successfully. A jsCall(url) is defined for client-side calling. CG defines two calling methods here, DOM method and XML method. The XML method can be used for low version browsers and Flash objects, and the DOM method is relatively simple, but the essence is the same. Flash communicates asynchronously with its container, so the DOM method call is implemented by the DOM object shielding the XML communication process. CG recommends using the XML method because it is more flexible. The XML call uses the following rules:
The arguments node is the parameter list and type
The download address of all source code examples above: http://www.lidaren.com/code/popup/src.zip