태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

ria/flex2008/02/13 11:49

팝업 차단을 통해 대부분의 브라우져에서 팝업을 차단한다.
일반적으로 navigateToURL 을 사용해서 팝업을 여는데, 팝업 차단기능 때문에,
원하는 동작이 이루어지지 않을수 있다.
이런 경우를 위한 팁이다.

윈도우의 IE7과 FireFox에서는 테스트 해보지 않았다고 하는군,,

flex-icon.png

Just about every application at some time or another needs to launch a new window... Its easy enough to open a new window. In flex, you can just use the navigateToURL window, but that has limitations. Nearly every browser now has a popup blocker on it. Unfortunately, navigateToURL fails silently when blocked by a popup blocker. Your browser might display an error message when the popup is blocked, but there is no way to know this within your application.

Well, with a small chunk of JavaScript, and ExternalInterface, you can detect when popup windows get blocked. Basically, instead of using navigateToUrl to launch a new window, you delegate this action to JavaScript's window.open function.

Whenever you use window.open, the open function will return a reference to the window that you just opened. If that reference is null, you know that the popup was blocked. Some browsers and popup blockers handle things slightly differently, so I also add a try/catch around the window.open function call. This way you can catch any errors opening the window. If the window opened correctly, have the JavaScript function return true to the Flex application. If it did not display correctly, have it return false to the Flex application.

Here's an example JavaScript function:

function openURL( url, target )
{
	try
	{
		var popup = window.open( url, target );
		if ( popup == null )
			return false;

if ( window.opera )
if (!popup.opera)
return false;
}
catch(err)
{
return false;
}
return true;
}

Now, in ActionScript you just need to invoke this function using ExternalInterface. Here's an example how:

public class URLUtil
{
	public static function openPopUp( url : String, target : String="_blank" ) : void
	{
		var success : Boolean = ExternalInterface.call( "openURL", url, target );
		if ( !success )
			Alert.show( "Request blocked.", "Error" );
	}
}

Notice that I am using the result of the ExternalInterface.call method to determine if the window opened correctly. If it returned false, then it didn't open correctly, and an Alert message is shown.

You can test it out for yourself here:

Note: You will need to disable popups for this site in order to see what happens when the popup is blocked. I've only tested this with IE7 and FireFox on Windows. Let me know if it fails on anything. :-)

This just goes to show you that its not always Flex vs. JavaScript. In order to achieve optimum results, you often need both.

http://www.insideria.com/2008/02/quick-flex-tip-detecting-popup.html

Posted by THLIFE.net

TRACKBACK http://thlife.net/trackback/148 관련글 쓰기

댓글을 달아 주세요

  1. 주봉이

    팝업을 닫을 경우에는 어떻게 해야 되나요? 중복해서 창이 뜨면 안될 경우에요.

    2010/07/07 09:44 [ ADDR : EDIT/ DEL : REPLY ]
  2. 東京風俗が満載のサイトです。東京の風俗が無料で探せます。割引特典が見つかります。

    2010/10/06 13:49 [ ADDR : EDIT/ DEL : REPLY ]