Sometime we need to get the url of the web page where our SWF is located.
Actionscript 3.0 gives us a way to do it just using 1 line code.
Just use ExternalInterface class that runs a Javascript command and gets the page’s url.
Here is the code:
1 2 | var pageURL:String=ExternalInterface.call('window.location.href.toString'); trace(pageURL); |
DEMO
In that case I called the property href of Javascript Location class using Actionscript.
Javascript Location class has its properties so I used hostname, pathname and protocol too.
Also I got data of the user’s browser using the Navigator class of Javascript with its properties userAgent and platform.
hurray
All this with Actionscript 3 !
1 2 3 4 5 6 7 8 9 10 11 12 | var pageURL:String=ExternalInterface.call('window.location.href.toString'); var pageHost:String=ExternalInterface.call('window.location.hostname.toString'); var pagePath:String=ExternalInterface.call('window.location.pathname.toString'); var pageProtocol:String=ExternalInterface.call('window.location.protocol.toString'); var userAgent:String=ExternalInterface.call('window.navigator.userAgent.toString'); var platform:String=ExternalInterface.call('window.navigator.platform.toString'); url_txt.text=pageURL; hostname_txt.text=pageHost; path_txt.text=pagePath; protocol_txt.text=pageProtocol; browser_txt.text=userAgent+"\n"+platform; |







{ 6 comments… read them below or add one }
wtfpixel 12.16.08 at 12:40 am
does not work. returns Parameter text must be non-null error.
admin 12.16.08 at 5:46 am
Works to me…
Are you trying it online ?
vicky 12.30.08 at 11:54 am
aaaaaaaaaaaaaaaaaaaaaaaaaaa
henry 10.10.09 at 10:33 am
not working…
admin 10.10.09 at 11:28 am
Works to me
Jocce 12.02.09 at 9:16 am
Nice stuff
I’m trying to get the description from a page into my flash, iv tried this as3 code, but cant get it to work.
var pageDESCRIPTION:String;
var metas:Array = new Array(ExternalInterface.call('document.getElementsByTagName("meta")'));
for (var x:int=0; x<metas.length; x++) {
if (metas[x].name.toLowerCase() == "description") {
pageDESCRIPTION = metas[x];
}
}
trace(description.content);
Any ideas?