0
votes

I would like to open external URLs from within my Flash project. I typically use something like this:

getURL("javascript:newwin=window.open('http://someurl.com','','');");

But, if Javascript isn't available (in cases where the SWF is not embedded in HTML) then this will fail. How can I detect whether Javascript is available? If it isn't available, I'd probably just use getURL and give it the direct URL, although I only want to do this if using the Javascript method isn't possible. Thanks!

2

2 Answers

5
votes

To accomplish what you're describing, the best way is to have Flash invoke a JavaScript function called "pingJavaScript". If JavaScript is running, that JavaScript function will then call a function on the Flash movie called "receiveJSNotification". So in your Flash movie, if that method gets called, you know JS is running.

To enable robust communication between a Flash movie and JavaScript, include this at the top of an Actionscript on the first frame of your movie:

import flash.external.ExternalInterface;
import flash.events.Event;

Add a function to receive a "yes, I'm alive" from JavaScript:

var js_available = false;

function receiveJSNotification(str:String):void {
 _root.js_available = true;
}

ExternalInterface.addCallback("notifyFlash", receiveJSNotification);
ExternalInterface.call("pingJavaScript", null);

In JavaScript:

function pingJavaScript()
{
    var movie = getFlash();
    movie.notifyFlash();
}

function getFlash()
{
    var movie = null;
    if (navigator.appName.indexOf('Microsoft') != -1) {
        movie = window['flashmovie'];
    } else {
        movie = document['flashmovie'];
    }
    return movie;
}
3
votes

I find that there is actually a much easier way to accomplish this:

public function isThereJavaScript():Boolean{
    return Boolean(ExternalInterface.call("window.location.href.toString"));
}

This requires no JavaScript back-end. It just works consistently. If there is the ability to call JavaScript, that means that there has to be a window property, which will have a non-null location.href. ExternalInterface is also available for both AS2 and AS3.