views:

35

answers:

2

I only have very limited control over a flash application. The only thing I can do is modify some links in an XML that it reads from.

I want to execute JS code based on the selected item in flash. I can link to something like: currentpage.html#some_anchor, but is there any way for me to detect the URL change from jQuery?

+1  A: 

In modern browsers you can listen to the onhashchange-event.

$(function() {
    $(window).bind('hashchange', function() {
        // more here
    });
});

In IE<8 you have to set an interval and poll if the hash has changed.

var i = window.setInterval('checkforhashchange', 500);
alopix
Forgot to write IE<8.
alopix
+2  A: 

As alopix mentioned, you can use the HTML5 event, onhashchange, for IE8+, Chrome 5+ and Firefox 3.6+.

Since you're already using jQuery, you might want to take a look at this plugin by Ben Alman, which makes an implementation of the event available for unsupporting browsers. This makes it as simple as:

$(window).hashchange(function () {
    alert(window.location.hash);
});

You can also use ExternalInterface.call() to invoke a JavaScript function from Flash, which is probably a lot simpler for what you need, but doesn't help much if you don't have access to the Flash code.

Andy E