Aug 03 2008

Opening popups from text links

Published by at 5:10 pm under General

I already have a post which shows how to open popups using getURL or navigatetoURL (this is the link) but what if you want to use html text to open a popup in Flash. Firefox blocks those popups and you cannot do anything about it, but there is a solution using javascript.

 

The trick is using javascript in href tag instead of normal http link, for eg, lets say you want to open http://www.nayansavla.com/blog, the normal code for html enabled text box would be,

 

sampleText.htmlText = “<a href=’http://www.nayansavla.com/blog’> My Cool Blog</a>”;

 

The above link might not work in all browsers, the solution to make this link work would be using javascript and External Interface. First we modify the link as shown below,

 

sampleText.htmlText = “<a href=’javascript:openwindow(” + “\”http://www.nayansavla.com/blog\”” +”)’>My Cool Blog</a>”;

 

Also we add few lines of extra code in our Flash file,

import flash.external.ExternalInterface;

ExternalInterface.addCallback("callGetURL",this,callGetURL);

function callGetURL(url:String){

getURL(url,"_blank");
}

 

The basic idea is we will first try to open a window with javascript but then if that fails we will call this function callGetURL using ExternalInterface. The javascript function openwindow looks as shown,

 

function openwindow(pageUrl) {
var winName = Math.round(9999*Math.random()) + new Date().getTime();
var winNew = window.open(pageUrl,winName,"toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=800,height=700,left=200,top=100");
if(!winNew) {
if(navigator.appName.indexOf("Microsoft") != -1) {
//href is the id of the flash element
window.href.callGetURL(pageUrl);
}else {
//href is the id of the flash element
window.document.href.callGetURL(pageUrl);
}
}
}

So now if your javascript doesn’t open a popup, flash geturl will :D. hope this helps. This doesn’t work with Firefox 3, will have to look more into it. Also on further looking into this, it displays inconsistent behavior in Firefox. :(
 

 

No responses yet

Trackback URI | Comments RSS

Leave a Reply