Popups Blocked in AS3 with navigateToURL()
Posted by: jordan in flash, tags: flash, javascriptBy default, browsers with a built-in popup blocker will usually block Flash navigateToURL requests that open in a “_blank” window. For example: navigateToURL(”http://www.google.com”, “_blank”); will generally get blocked. Even if you have a mouse click event to send off the navigateToURL call, it will be blocked.
The good news is that you can get around this behaviour by calling out to JavaScript to handle the request for you. Go ahead and download URLNavigator.as and save it as ~/com/zorked/URLNavigator.as, where ~ is the folder that your FLA or Flex project resides within. In your ActionScript code, import com.zorked.URLNavigator, then call out to method with URLNavigator.ChangePage(”http://www.example.com”, “_blank”); in the mouse event that you’re capturing.
Now, I’ve tested this with a variety of systems, and it works on a lot of them but it’s not foolproof. So far, it works in IE 6, 7, & 8 (beta), Firefox 2 & 3, Opera 9.x, Safari 3, and runs on XP, Vista, and OSX. It may work on additional systems, but my testing is limited. You may also have problems if you embed your SWF in a strange way. It seems to work with most JavaScript embed libraries, but different parameters on the <object> and <embed> could conceivably cause problems.
However, the caveat is this: you CANNOT call this code except within a click mouse event that you’re listening for. Popup blockers will rightfully block an automatic popup, and there is no way around it. If you ask me how to do an auto-popup, I’ll find your house while you’re sleeping; this method is for white-hat developers looking to open a new browser window for good reason.
Additionally, I made an update to the link that was originally saved: Wordpress likes to save files as lowercase names, which Flash had problems with, since the file didn’t match the name of the class. You should be able to download and work with it without having to rename it.
I’m open to suggestions and improvements, and if anyone has access to additional browsers or operating systems that it’s worked for them on, please let me know.
[...] http://www.zorked.com/flash/flash-and-navigatetourl-popup-blocking/ [...]
Thanks
worked perfectly.
Thanks too
Forgive this newby, but where do I place the .as file?
And finding this was a GodSend!!! Thanks. WCW
Hi Willow,
You can save the .as file into /com/zorked/URLNavigator.as, relative to your .fla. So, if your .fla is at /Documents/MyApp.fla, you would save it as /Documents/com/zorked/URLNavigator.as.
In your .fla or other .as files, you’ll import com.zorked.URLNavigator.
Thanks for the start. Now what do you mean by “import com.zorked.URLNavigator?
Here is my simple code. There is an image in a frame that I’ve made into a button, and named an instance of that button. I want the button to link to an outsie URL…..so simple.
Code:
dooWop_btn.addEventListener(MouseEvent.CLICK, goToTvPledge);
function goToTvPledge(e:Event):void
{
var tvPledgeRequest:URLRequest = new URLRequest(”https://pledge.wusf.usf.edu/wusftvdefault.asp?p=TVWEB”);
navigateToURL(tvPledgeRequest);
}
When I use it, I get the Pop-blocked error….I just need to get this link to work.
I’ve downloaded your .as file and placed it where you suggest. Now what? How do I modify my code so that it will activate or whatever it’s supposed to do in relationship to that.as file? Or does the .as file replace it? Am I supposed to modify something on that file or in my code?
And why is this so complicated? All I want to do is make a few frames in my flash document link to an outside Website.
Sorry to be dumb about this, and whatever you can do to assist me, I will appreciate. WCW
The first line in your frame will need to be:
import com.zorked.URLNavigator;
This tells Flash to compile that file into the SWF so that the rest of your code in the .fla can use it.
Then, replace the code inside of your goToTvPledge() function with:
URLNavigator.ChangePage(”https://pledge.wusf.usf.edu/wusftvdefault.asp?p=TVWEB”, “_blank”);
The reasons that this process is complicated are
1. Programming Is Hard
2. Flash is primarily an animation platform that’s had a programming framework crowbared in, so wrapping your head around how Flash expects things takes some time.
I suggest researching Document classes. That helps clean up your code by keeping it separated from your UI, the .fla.
Wow! Thanks so much for your quick and generous reply. I’ll give it a try. And a hearty LOL about programming is HARD!
I will keep at it, for sure! and I will definately continue to study.
Gratefully, WCW
I am getting these errors in Flash when I place the code you suggest.
1093: Syntax error.
1084: Syntax error: expecting rightparen before colon.
This is what I wrote: Have I placed the code in the wrong place?
adjusted Code:
import com.zorked.URLNavigator;
dooWop_btn.addEventListener(MouseEvent.CLICK, goToTvPledge);
function goToTvPledge(e:Event):void
{
URLNavigator.ChangePage(”https://pledge.wusf.usf.edu/wusftvdefault.asp?p=TVWEB”, “_blank”);
}
Thanks, WCW
The code is correct, but Wordpress is formatting the quotes, so Flash doesn’t recognize them. Just replace the fancy quotes with normal ones. Retyping them in your text editor should be fine.
Thank you so much for your patience and assistance. Everything is working fine, now.
It was working in the standalone HTML file that Flash produces, when you publish the .fla, but not when I embedded it in the actual page it needed to appear in. But I found that I was still using old code from AS2 days to embed the file, with a hack to resolve some IE issues.
Once I replaced the Embed code, all is working fine, now.
I am most grateful. Thanks again. WCW
Been working with this all day. This and similar code do not work on Vista — both IE and Firefox block the popup…
Hi Greg,
I’ve tested this code on a variety of machines, including Vista. Unless there’s been a very recent update to IE popup blocking, it should work.
My guess is that for some reason, ExternalInterface.available is returning null for you. It may do that if you’re just opening an HTML file on your hard disk rather than over localhost, or if Flash isn’t recognizing the ExternalInterface because of the way you’re embedding the SWF onto the page.
If you want to email your files to me, I’ll take a look. jordan@zorked.com
it’s not working..
I need more info than that Vincent. Can you tell me what errors you’re getting and how you’re embedding your SWF into the HTML?
nop . it is not working !!
It works for me with the latest FireFox (2.0.016) but not in IE7 any suggestions?
Link 404 not found
This works well, including for Safari on Windows and Mac. Appreciate it!
Great, Thanks for the script. I’m having a small issue, maybe you can help me out. I’ve looked everywhere for the fix but it’s been kicking my butt for a couple of days. I’m pretty new to Flash and Actionscript.
I’ve placed the folder in the place (screenshot)
http://www.broketravelers.com/screenshot.jpg
and I import it.
import com.zorked.*;
-or-
import com.zorked.navigatetourl;
but when i call the function:
private function runOnce(event:TimerEvent):void {
URLNavigator.ChangePage(”http://www.adobe.com”);
}
I get the code:
Error #1065: Variable URLNavigator is not defined.
Head scratcher for me. Any help is Appreciated. I dont think I’m importing it correctly. But other .as files I am importing are importing correctly. Thanks a bunch!
I keep getting a variable URLNavigator is not defined. Everything is imported correctly. any ideas?
Samantha, you’ll need to put the class in a /com/zorked/ folder relative to your FLA. Then, you can import com.zorked.URLNavigator.
Like this
MyFolder/MyFile.fla
MyFolder/com/zorked/URLNavigator.as
Do i need to change the paramaters inside of URLNavigator or is it fine how it is?
You shouldn’t have to edit URLNavigator. You can pass either a URLRequest object or a string, along with the window you want it to open in (usually “_blank” for a new window).
I suppose I ought to edit this post with comments and usage notes, but you can just do:
URLNavigator.ChangePage(”http://www.google.com”, “_blank”);
Great! thanks! works like charm. I saved the .as file as urlnavigator.as instead of URLNavigator.as
Silly me! keep up the good work!
but window.open also gets blocked by popupblockers!!!
[...] http://www.zorked.com/flash/flash-and-navigatetourl-popup-blocking/ [...]
Hi, this solution not longer works in Firefox version 3 if the pop-up blocker is on. It seems that Firefox version 3 is more stringent with pop-ups.
Mike, I’ve just now tested it and it worked. This method will only work if you call URLNavigator.ChangePage within a MouseEvent.CLICK listener; is that possibly the reason it’s not working for you?
jordan,
wow, this is a really great way to get pop ups!!! I have been working on this for a few days now and your way works. was wondering if there is any way to control the window even more, such as size, location where the window pops up? if you know how and any more attributes please do tell … thank you again … themadcow
Cow, you can modify URLNavigator to change the ExternalInterface.call line.
In JavaScript, window.open looks like this: window.open(url, windowName, features);
Take a look at Mozilla’s docs for what you can put for “features”: http://developer.mozilla.org/en/DOM/window.open
So in Flash, the ExternalInterface.call might look like this:
ExternalInterface.call(”window.open”, req.url, window, “width=400,height=500,top=50,left=200″);
I’ll probably modify URLNavigator so that you can pass that stuff in–good idea.
jordan,
i tried to replace this in the URLNavigator and I found no result? i am actually trying to make a navigation with three buttons that all call javascript pop ups? I noticed that if you try to copy and paste the code for the next button in the navigation and you receive a duplicate function issues as well. is there anyway you can send me a tid-bit of code regarding this first issue and now the following issue? thank you.
kind regards … cow