Can an SWF file read the filename of the XHTML file that loaded it?
June 24, 2004 1:02 PM   Subscribe

Actionscript: Is there any way for a SWF file to read the name and/or path of the XHTML file that loaded it?
posted by signal to Computers & Internet (9 answers total)
 
This is a great question, and I'm sure it's possible. I will try to cobble something together, but for now I'll leave you with these thoughts:

-- there ISN'T an actionscript command that does what you want (as far as I know).

--BUT there IS a Javascript command that does it: try typing the following into your browser's URL field:

javascript:alert(window.loation.href)

You should get an popup window telling you the URL of the current page.

-- So this means that you could write a javascript function THAT LIVES ON THE WEBPAGE -- NOT IN THE SWF -- that detects the current URL.

-- there's and Actionscript command called FSCommand that can run Javascript functions on the webpage that houses the swf. So you could get the flash movie to run the Javascript function that returns the url.

-- then you've have to get this data back into the flash movie. It's possible to do that too, but I forget how. As I said, I'll work on it.

--Meanwhile, here's some info from Flash's help system about fscommand:

In the HTML page that embeds the SWF file, add the following JavaScript code:
function theMovie_DoFSCommand(command, args) {
if (command == "messagebox") {
alert(args);
}
}

If you publish your SWF file using the Flash with FSCommand template in the HTML Publish Settings dialog box, this code is inserted automatically. The SWF file's NAME and ID attributes will be the filename. For example, for the file myMovie.fla, the attributes would be set to myMovie. (For more information about publishing, see Publishing.)

Alternatively, for Microsoft Internet Explorer applications, you can attach an event handler directly in the tag, as shown in this example:

[Script Language = "JavaScript" event="FSCommand (command, args)" for= "theMovie"]
...
[/Script]

In the Flash document, add the fscommand() function to a button, as shown in this example:
on(press) {
fscommand("messagebox", "This is a message box invoked from within Flash.");
}

You can also use expressions for fscommand() and parameters, as in this example:

fscommand("messagebox", "Hello, " + name + ", welcome to our website!")

Select File \ Publish Preview \ HTML to test the document.
posted by grumblebee at 2:09 PM on June 24, 2004


Response by poster: that is such a good answer. I will be very, very grateful if you "work on it" and help me fix this issue. I am working on a website with a flash-based menu, and an unnatractive, not very efficient sharedobject-based system to keep track of where the user is in the navigation. Problem is, when you get to a page via an html link, or a back button, the menu doesn't change to reflect this.

One question, would it be possible to pass a variable to flash in the EMBED tag, and have said variable be generated by the javascript window.location.href property?
posted by signal at 2:26 PM on June 24, 2004


Response by poster: Ok, this works:



<script LANGUAGE="JavaScript"> -->

document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="720" height="125"> <param name=quality value=high /><param name="movie" value="Menu.swf?topicname='+window.location.href+'" /><embed src="Menu.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="720" height="125"></embed> </object>');

</script>

posted by signal at 3:54 PM on June 24, 2004


Response by poster: Notice the "window.location.href" in the middle of that jumble.

One remaining question, how would I make sure that non-javascript browsers still see the flash file? I'm sure this is trivial, but I have next to no javascript experience.
posted by signal at 3:55 PM on June 24, 2004


Put your EMBED (without the javascript "window.location.href") in a noscript tag after your script.

Example:

< script>
... what you have above ...
< / script>
< noscript>
... standard object/embed ....
< / noscript>

Of course, the navigation thing still won't work without javascript.
posted by mrgavins at 5:28 PM on June 24, 2004


Are you really that concerned about non-JavaScript browsers? I mean, your nav is in Flash to begin with, which will probably already rules out the Luddite crowd.
posted by sad_otter at 7:24 PM on June 24, 2004


Non-Javascript... maybe he wants search engines?

ps. Only rotters hunt otters.
posted by holloway at 7:39 PM on June 24, 2004


Response by poster: in case anybody cares, here's the test site.

After you get past the entrance, the menu should show you where you are in the navigation (only the first 3 topics have sub-topics, though).

sad_otter: some people might surf with javascript turned off, no? Just covering as many bases as possible.3

Please let me know if it doesn't work on your platform/browser combo.
posted by signal at 9:14 PM on June 24, 2004


Non-Javascript... maybe he wants search engines?

Will a search bot parse a SWF to navigate? I doubt it.

I was just pointing out that the Flash-enabled + JavaScript-disabled crowd is probably pretty small (and used to things not working quite right). But, yeah, adding a noscript is pretty trivial.
posted by sad_otter at 7:42 AM on June 25, 2004


« Older Zelda heart t-shirts   |   Can anyone recommend a good read on Modern... Newer »
This thread is closed to new comments.