views:

1784

answers:

5

Hello Everyone,

I wrote a flex application that get the host string from the browser using this code

ExternalInterface.call("window.location.host.toString")

This line of code work prefectally to get the host string in both Firefox and Opera. However, when using IE, the returned string is always 'null'. I need to get such information from the browser. I know that there is a work around by defining a javascript function that get such string and calling that function from the application. However, my application require getting such information from a native source.

I was wondering if anyone had the same problem and managed to solve it, or if someone has any idea why I always get null in IE, but not when using Firefox and Opera

Edit 1:

Here is the HTML code for embedding the generated SWF file. Maybe this is useful to spot a mistake

<object id="myTest1" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" height="330px" width="600px">
<param name="movie" value="http://www.website.com/test.swf" />
<param name="allowScriptAccess" value="always" />
<param name="wmode" value="transparent" />
<embed id="myTest1" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://www.website.com/test.swf" allowScriptAccess="always" wmode="transparent" height="330px" width="600px" flashvars=""></embed>
</object>

the id, classid, and the allowScriptAccess are set as shown

Any idea?

Edit 2:

for Lior Cohen The Flex file is the example used in your first link. The sub-directory history contains history.js, history.css, and historyFrame.html. The HTML page that include the generated SWF file is like this

<html>
<head>
<!--  BEGIN Browser History required section -->
<link rel="stylesheet" type="text/css" href="history/history.css"/>
<script src="history/history.js" language="javascript"></script>
<!--  END Browser History required section -->
</head>
<body>
<object id="file1" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" height="330px" width="600px">
 <param name="movie" value="file.swf" />
 <param name="allowScriptAccess" value="always" />
 <param name="wmode" value="transparent" />
 <embed id="file2" pluginspage="http://www.macromedia.com/go/getflashplayer" src="file.swf" allowScriptAccess="always" wmode="transparent" height="330px" width="600px" flashvars=""></embed>
</object>
<body>
</html>

However, this is still not working as expected.

Edit 3:

I have spotted the problem, however, I cannot fix it. The problem has to do with the javascript engine of IE and not the ExternalInterface nor the object and embed HTML tags.

What I am doing in my case is write the object and embed tags into a div created using javascript and this div is appended to the end of the body using the DOM methods. However, such approach make the InternalInterface always return null in IE (but not in Firefox nor in Opera).

var swfDiv = document.createElement('div');
swfDiv.innerHTML = '<object id="test1" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="600" height="330"><param name="movie" value="http://www.website.com/test.swf" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="quality" value="high" /><embed id="test2" name="test2" src="http://www.website.com/test.swf" allowScriptAccess="always" allowFullScreen="false" quality="high" width="600" height="330" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
document.body.appendChild(swfDiv);

I tried to use document.write to append the HTML content, which made it work perfectly in IE, however, document.write wrote over the entire page (removing old content), which is something I don't want.

document.write('<object id="test1" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="600" height="330"><param name="movie" value="http://www.website.com/test.swf" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="quality" value="high" /><embed id="test2" name="test2" src="http://www.website.com/test.swf" allowScriptAccess="always" allowFullScreen="false" quality="high" width="600" height="330" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');

Any idea how to fix this?

Thanks

A: 

A quick search reveals you will have to set some extra attributes on the object to make it work in IE. Set id classId and scriptAccess (the last one to 'true' of course) to get this to work. (google for more info)

(Not tested.)

http://www.google.com/search?q=externalinterface+internet+explorer

Simon Groenewolt
can you be more clear, or provide links or the used search keywords. if you mean the allowScriptAccess="always" in object and embed, then yes I already done that. calling javascript is working, however, the values that are returned from the javascript functions are always null.
google search added to answer
Simon Groenewolt
So if you've already set allowScriptAccess, did you try and add the id and classId attributes?
Simon Groenewolt
yeah, the id, the classid, and the allowScriptAccess are all set in both the object and embed tags. However, this is still not working in IE :(
Sorry, in that case I don't have any additional suggestions, except trying a few different calls on ExternalInterface, and (more important) make the ids of the embed and object element different - making them the same is not valid or at least could be confusing (to the browser).
Simon Groenewolt
No worries, I really appreciate your suggestions. And yes you are right, the id should be different.
+1  A: 

Take a look at the following link. It should provide you with what you're looking for, without using ExternalInterface.call().

http://livedocs.adobe.com/flex/3/html/help.html?content=deep%5Flinking%5F7.html

As mentioned in the page above, for the BrowserManager class to offer its full functionality, the wrapper must include several supporting files (history.js, amongst others).

More information about how to obtain and use these supporting files can be found in the following link under the "Deploying applications that use deep linking" section.

http://livedocs.adobe.com/flex/3/html/help.html?content=deep%5Flinking%5F2.html

Lior Cohen
This looks like a good solution.
Simon Groenewolt
Hmmm, I was happy when I saw such native support. However, I tried the example in that page using Firefox and IE, and it didnt get any information about the URL. have you tried the same example and got any results?
Did you notice the comments at the bottom of the page in the provided link?
Lior Cohen
I am sorry, but maybe I am missing something. I have no idea what is this history.js file. I am using a the flex SDK and code using a text editor and compile with mxmlc. I dont know where to find this history.js to include it. any idea?
Updated the answer with more information.
Lior Cohen
Sorry man. I tried to include the history.js (and other related files) as mentioned in the link, however, nothing seems to be changed. It seem to me that such feature is only workable to detect URL changes that should be made by the application and to track the history of the current website. But I might be wrong
Could you share some of the code you're using that doesn't work? I have used these features before and they worked fine.
Lior Cohen
better, can you share a working code since you have one?Anyway, check my last edit to the question, I added the code I used to test the suggested method as you requested.
A: 

The solution to the problem that is mentioned in "Edit 3" is basically to create the object tag and its parameter tags using DOM objects, instead of just specifying them as string for the innerHTML attribute. This will make the returned value of the ExternalInterface work in Internet Explorer. Here is an example:

var swfDiv = document.createElement('div');

var obj = document.createElement('object');
obj.setAttribute('id','test1');
obj.setAttribute('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0');
obj.setAttribute('width','600px');
obj.setAttribute('height','330px');

var param = document.createElement('param');
param.setAttribute('name','movie');
param.setAttribute('value','http://www.website.com/test.swf');
obj.appendChild(param);

param = document.createElement('param');
param.setAttribute('name','allowScriptAccess');
param.setAttribute('value','always');
obj.appendChild(param);

param = document.createElement('param');
param.setAttribute('name','wmode');
param.setAttribute('value','transparent');
obj.appendChild(param);

swfDiv.appendChild(obj);
document.body.appendChild(swfDiv);

document.getElementById('test1').setAttribute('classid','clsid:d27cdb6e-ae6d-11cf-96b8-444553540000');

Note 1: This will make it work with Internet Explorer, any other browser should use any of the method mentioned above in "Edit 3" of the question. You can detect the browser and use the proper code accordingly.

Note 2: the last line is required to make it work properly, and it has to be like that and at the end after adding the object to the document. I do not know why, probably it has to do with the weird browser behavior.

A: 

@AAA

Note 2: the last line is required to make it work properly, and it has to be like that and at the end after adding the object to the document. I do not know why, probably it has to do with the weird browser behavior.

Thank you very much ! I suppose it is a bug in IE. I wonder how you found out.

the tedious trial and error :)I can explain why it should be that way, but I am just happy that it worked, and even more happier that I was able to make someone happy as well :)
A: 

Using SWFObject (you'll find it at http://code.google.com/p/swfobject/) to place the Flash on the page also solves this problem in Internet Explorer.

Fredrik