I have a Flash movie on a web page and I want to send parameters to it with JavaScript to have it go to a selected frame. The AS3 code receives its information via an ExternalInterface API. The following code parses the URL argument (?page=x) and I want to send the value (x) to Flash. Unfortunately, when creating the objMovie variable, it is always undefined, therefore getFlashMovie is undefined and Flash never receives the callback.

Thinking that it was because the flash movie is not loaded yet, I created a button to call callToActionscript, and when I click the button, I get the same error. (getFlashMovie("myFlashMovie") is undefined)

I am not seeing what I'm doing wrong (mainly because I'm a newbie...). Please enlighten me.

Also,

if (params)

always returns true, whether there are parameters or not. I don't understand that, either.

The code follows:

<script >  
$(document).ready(function() {
	var params=getQueryParameters();
	if(params) {
		callToActionscript(params[0]);
	}	
});

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  var objMovie = (isIE) ? window[movieName] : document[movieName];
  alert(objMovie); //Always return undefined
  return objMovie;
}

function callToActionscript(page) 
{
	var myMovie=getFlashMovie("myFlashMovie").sentToActionscript(page);
}

function getQueryParameters() {
  var query = window.location.href.split('?')[1];
  //query won't be set if ? isn't in the URL
  if(!query) {
    return { };
  }
  else
  {
    var param = query.split('=');
  }
  return param[1];
}
</script>

CNIDog,

If you simply need to test whether a query string is present (regardless of its content), then:

$(document).ready(function() {
	if(location.search.replace('?', '') !== '') {
		callToActionscript(params[0]);
	}	
});

If however, you need to test for a pericular parameter in the query string then:

$(document).ready(function() {
	if(getQueryParameters(myParamName) == 'myValue') {
		callToActionscript(params[0]);
	}	
});

function getQueryParameters(name) {
  var queryString = location.search.replace('?', '');
  var args = queryString.split("&");
  for (var i = 0; i < args.length; i++) {
    var pair = args[i].split("=");//Split into name|value pairs
    if (pair.length >= 1) {
      if(pair[0] == name) return pair[1];
    }
  }
  return false;
}

On the assumption that movieName is an element id, then try this:

function getFlashMovie(movieName) {
  var objMovie = $(movieName);
  return objMovie;
}

It's possible that $(document).ready is too early an event for interacting with the movie, in which case try $(document).load (I think).

You may need to write callToActionscript(page) as a VB script rather than javascript. This is certainly so going the other way - calling a function from an action script.

Probably not a complete answer but should give you a few pointers.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.