views:

140

answers:

4

So, this code splits the screen into two frames:

Javascript:A14nH=location.href;L3f7="http://www.google.com";R1Gh7="http://www.google.com";if(L3f7&&R1Gh7){Fr4Q='<frameset%20cols=\'*,*\'>\n<frame%20src=\''+L3f7+'\'/>';Fr4Q+='<frame%20src=\''+R1Gh7+'\'/>\n';Fr4Q+='</frameset>';with(document){write(Fr4Q);void(close())}}else{void(null)}

I need to run this code from a flex application and what I am doing is:

   var jsFunc:String = "function () { location.href=\"javascript:A14nH=location.href; L3f7=\"http://www.google.com\";R1Gh7=\"http://www.google.com\";if(L3f7&&R1Gh7){Fr4Q=\"<frameset%20cols=\"*,*\">\n<frame%20src=\"\"+L3f7+\"\"/>\";Fr4Q+=\"<frame%20src=\"\"+R1Gh7+\"\"/>\n\";Fr4Q+=\"</frameset>\";with(document){write(Fr4Q);void(close())}}else{void(null)}\"}";
   var divExists:Boolean = ExternalInterface.call(jsFunc);

Can someone tell me what's wrong?

(Btw, if you change var jsFunc:String = "function () { location.href='http://www.google.com' }"; the page does take you to Google)

+3  A: 

For one, make sure your "allowNetworking" parameter of the object and embed tags of Flash is set to true. Else, the whole ExternalInterface package becomes severely limited.

Second, why are you trying to store the javascript in AS3? Why not put the javascript function in your html directly? Then all you have to do is ExternalInterface.call(myJavascriptFunc).

Jonathan Dumaine
How do you exactly do that?
Just to clarify, I am not embedding flash in HTML, I am building a flex application (flash application using the flex framework).
+1  A: 

I believe you can call the eval function and pass it the JavaScript you want it to execute:

var jsFunc:String = "location.href=\"javascript:A14nH=location.href; L3f7=\"http://www.google.com\";R1Gh7=\"http://www.google.com\";if(L3f7&&R1Gh7){Fr4Q=\"<frameset%20cols=\"*,*\">\n<frame%20src=\"\"+L3f7+\"\"/>\";Fr4Q+=\"<frame%20src=\"\"+R1Gh7+\"\"/>\n\";Fr4Q+=\"</frameset>\";with(document){write(Fr4Q);void(close())}}else{void(null)}\"";
var divExists:Boolean = ExternalInterface.call("eval", jsFunc);
James Ward
A: 

Try removing the first location.href assignment and leave the code you want executed:

var jsFunc:String = "function () {
    lA14nH = location.href;
    L3f7 = \"http://www.google.com\";
    R1Gh7 = \"http://www.google.com\";

    if (L3f7 && R1Gh7) {
        Fr4Q = '<frameset%20cols=\'*,*\'>\n<frame%20src=\''+L3f7+'\'/>';
        Fr4Q += '<frame%20src=\''+R1Gh7+'\'/>\n';
        Fr4Q += '</frameset>';

        with (document) {
            write(Fr4Q);
            void(close())
        }
    }
    else {
        void(null)
}";
var divExists:Boolean = ExternalInterface.call(jsFunc);
Ast Derek
A: 
  1. You are not escaping quotes propertly.
  2. ExternalInterface.call() expects a javascript statement to execute - you're passing the definition of a function, but not calling it. As already suggested, append a () to the end to call the passed function.

I believe I've escaped the quotes properly. Try this and let me know what happens.

var js:String = "function () { ";
js += "location.href=\"";
js += "javascript:A14nH=location.href;";
js += "L3f7 = \"http://www.google.com\";";
js += "R1Gh7 = \"http://www.google.com\";";
js += "if(L3f7&&R1Gh7){ ";
js += "Fr4Q= \\\"";
js += "<frameset cols='*,*'><frame src='\\\" + L3f7 + \\\"'/>\\\"; ";
js += "Fr4Q+=\\\"<frame src='\\\" + R1Gh7 + \\\"'/>\\\"; ";
js += "Fr4Q+=\\\"</frameset>\\\"; ";
js += "with(document){write(Fr4Q);void(close())} ";
js += "}else{void(null);} "
js += "\"} ()";
var divExists:Boolean = ExternalInterface.call(js);

If you pass

"alert('something');"

to ExternalInterface.call, it will execute it and you'll get an alert. But if you pass

"function(){alert('something');}"

it just creates an anonymous function. To call the function, you have to add () to it as in

"function(){alert('something');}()"

This is same as creating a named function and calling it like:

function test(){alert('something');}
test();

you can even pass parameters this way:

"function(text){alert('Got ' + text);('taDaa!!')}"
Amarghosh