View Full Version : JavaScript function via AS3 URLRequest
xwielder
05-12-2008, 08:37 PM
Okay, here's what I have:
var aboutURL:URLRequest = new URLRequest("onClick=\"frames[\'iframe1\'].location.href = \'about.html\'; return false\"");
navigateToURL(aboutURL);
But of course, it doesn't work.
Within a normal .html file it works. Like this:
<TD COLSPAN="5"><A HREF="#About" onClick="frames['iframe1'].location.href = 'about.html'; return false" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('TheGame','','navbar_images/TheGame_f2.jpg',1);"><IMG NAME="TheGame" SRC="navbar_images/TheGame.jpg" WIDTH="63" HEIGHT="14" BORDER="0" ALT=""></A></TD>
I'm not concerned with onMouseOut or onMouseOver, just the onClick.
So, what's the proper syntax/function to call a JavaScript function within AS3?
Slowburn
05-12-2008, 09:13 PM
you should use the ExternalInterface to talk with Javascript from Flash.
xwielder
05-12-2008, 09:33 PM
you should use the ExternalInterface to talk with Javascript from Flash.
Aye, I'm studying it now to see what I need to do to get this to work. Thanks for the reply.
xwielder
05-12-2008, 09:59 PM
umm, yeah, okay, sure. I think I'll forget it and just stick with a non-flash site. I didn't know you had to have 6 different functions and over 100 lines of code to do one simple javascript onClick function (as I exampled above). Unbelievable.
all you need is one line.
xwielder
05-13-2008, 01:59 AM
all you need is one line.
Not according to Adobe.
http://livedocs.adobe.com/flex/3/html/help.html?content=19_External_Interface_09.html
It looks like you'll need 3 .as (class) files, and a whole lot of event listeners on the main timeline of the .fla
Now, maybe I'm reading/understanding Adobe wrong, but it certainly looks like hoops of fire to jump thru to get this to work, whereas skipping flash altogether and just using one javascript line of code on my html page is a whole lot easier.
It can just be one line;
var query:String = flash.external.ExternalInterface.call("window.location.search.substring", 1);
xwielder
05-13-2008, 01:38 PM
It can just be one line;
var query:String = flash.external.ExternalInterface.call("window.location.search.substring", 1);
Okay, I suppose I'm still a bit confused.
Do I do this:
stop ();
import flash.external.ExternalInterface;
theGame.addEventListener (MouseEvent.CLICK, gameCLICK);
var query:String = ExternalInterface.call("onClick=\"frames[\'iframe1\'].location.href = \'about.html\'; return false\"", 1);
var aboutURL:URLRequest = new URLRequest(query);
function gameCLICK (event:MouseEvent):void
{
navigateToURL (aboutURL);
}
or this:
stop ();
import flash.external.ExternalInterface;
theGame.addEventListener (MouseEvent.CLICK, gameCLICK);
function gameCLICK (event:MouseEvent):void
{
ExternalInterface.call("onClick=\"frames[\'iframe1\'].location.href = \'about.html\'; return false\"", 1);
}
because truthfully, neither one works.
(just to note, I do appreciate the help everyone's provided. Please pardon my frustration and seemingly aggravated demeanor)
amarghosh
05-13-2008, 02:02 PM
write code in a method and call it:
//in the script.js file or html embedded script:
function jsFunction()
{
frames['iframe1'].location.href = 'about.html';
return false;
}
//from ur AS file:
var result:Boolean = ExternalInterface.call("jsFunction");
//u can specify additional arguments to ExtInt.call() and those will be used as parameters of called javascript function;
//and ExtInt.call will return the return value of js method if the call succeeds or else it returns null;
if it fails just see what ExternalInterface.available traces;
xwielder
05-13-2008, 02:15 PM
In my html page:
<SCRIPT LANGUAGE="JavaScript">
function jsFunction()
{
frames['iframe1'].location.href = 'about.html';
return false;
}
</SCRIPT>
And yes, I do have "<param name="allowScriptAccess" value="always" />" in the html <OBJECT> parameter.
In my swf:
stop ();
import flash.external.ExternalInterface;
flash.system.Security.allowDomain ("*");
theGame.addEventListener (MouseEvent.CLICK, gameCLICK);
function gameCLICK (event:MouseEvent):void
{
var result:Boolean = ExternalInterface.call("jsFunction");
trace (ExternalInterface.available); // Trace = TRUE
}
Everything looks correct, but nothing happens when I click the button.
amarghosh
05-13-2008, 02:32 PM
give ab alert from js function to make sure its being called;
xwielder
05-13-2008, 02:59 PM
In my html page:
<SCRIPT LANGUAGE="JavaScript">
function jsFunction()
{
alert "TESTING";
}
</SCRIPT>
Okay, this is a bit odd. So far, I've been testing everything with Firefox. I just decided to test this round with IE and when I clicked on the button, flash player threw up this lovely error:
SecurityError: Error #2060: Security sandbox violation: ExternalInterface caller
file://C:\Documents and Settings\user\My Documents\WEBSITE\flash\Site_Main.swf cannot access
file://C:\Documents and Settings\user\My Documents\WEBSITE\index.html.
at flash.external::ExternalInterface$/_initJS()
at flash.external::ExternalInterface$/call()
at Site_Main_fla::MainTimeline/gameCLICK()
xwielder
05-13-2008, 03:15 PM
Okay, after some research I found this:
http://www.mail-archive.com/flexcoders@yahoogroups.com/msg29394.html
Which states:
I came to the conclusion that it is impossible to call an external function from within Flash/Flex if the SWF file and the container HTML file are located on the local hard disk, rather than on a remote web server – with the exception that it will work in the location to which the SWF application was compiled, i.e. your project’s bin folder. If anyone can prove me wrong, I’d be glad to see how.
Okay, so I put the .fla in the same directory as my index.html file and re-compiled the swf. I then of course reflected this new swf path in my html.
On testing, IE no longer gives me the Security Sandbox error, but now it just does nothing when the button is clicked. Same with Firefox.
wvxvw
05-13-2008, 03:49 PM
var script:XML = <script>
<![CDATA[
function nav(){
document.location.href = 'http://www.actionscript.org';
}
]]>
</script>;
function navigateToActionscriptORG(evt:MouseEvent):void {
ExternalInterface.call(script);
}
stage.addEventListener(MouseEvent.CLICK, navigateToActionscriptORG);
This worked for me in IE6 and FF. I don't know if this'll work in IE7/8 and I can't test it at the moment...
xwielder
05-13-2008, 04:19 PM
var script:XML = <script>
<![CDATA[
function nav(){
document.location.href = 'http://www.actionscript.org';
}
]]>
</script>;
function navigateToActionscriptORG(evt:MouseEvent):void {
ExternalInterface.call(script);
}
stage.addEventListener(MouseEvent.CLICK, navigateToActionscriptORG);
This worked for me in IE6 and FF. I don't know if this'll work in IE7/8 and I can't test it at the moment...
and there we have it.
works wonderfully.
A sincere thank you.
amarghosh
05-14-2008, 03:53 AM
var script:XML = <script>
<![CDATA[
function nav(){
document.location.href = 'http://www.actionscript.org';
}
]]>
</script>;
function navigateToActionscriptORG(evt:MouseEvent):void {
ExternalInterface.call(script);
}
stage.addEventListener(MouseEvent.CLICK, navigateToActionscriptORG);
wow!! so the js code need not be in the html page? we can have it in AS itself? that is cool.... i didn't know that.... thanks wvxvw
wvxvw
05-14-2008, 05:36 AM
Actually, there are some limitations, like for example, I hadnt succeeded to change cursor style (for the reason I don't know =), but this is probably some minor issue, I'm not sure about how would even the basic functions work like getElementById() etc...
amarghosh
05-14-2008, 05:48 AM
while we r on the topic,
i got different responses when i called the same external js method from html page and the embedded flash;
say i had this function in the html page -- loaded from script.js;
function gotoPage(url)
{
newWindow=window.open(url,'_blank');
if(!newWindow)
return false;
newWindow.focus();
return true;
}
when i called this function from onclick of an html element the url was opened in new tab (and browser didn't complain). But when i called the same thing from an swf embedded in same html page using ExternalInterface.call, browser blocked opening in new tab saying its a pop up (it works if its '_self', but i want it in a new tab);
do u have any idea why my browser (latest FF on win xp) doesn't like calls from flash but it allows calls from html? --- After all its the same javascript method, right?
wvxvw
05-14-2008, 11:03 AM
It needs actual mouse-click to open or close window. This way it tries to prevent sites from opening numerous popups etc. But, probably, it doesn't catch mouse events/state inside activeX controlls... this is still a guess, I may be wrong.
amarghosh
05-14-2008, 11:07 AM
Thanks. that should be it... it makes sense having such a restriction...
but unfortunately it prevents flash from opening the 'clicked' links in a new tab :(
vyellep
06-05-2008, 10:38 PM
If you are testing locally "file://....." then the SWF should be local-file-system sandbx. In other words you need to set the -use-network=false . This flag is set to true by default so that ur app can use network resources. That should fix your sandbox violation issue
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.