[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Read URL from browser

Posted on 2010-01-01
Medium Priority
Last Modified: 2013-11-11
Im trying to do two things.
1. I'm trying to read the URL of the browser with the following (below) code snippet.
This is working, but with some glitches.
When I call the .swf file directly it won't work in IE. In Firefox it's giving me the URL correct.
Is there some other way of doing this, which will make it work called directly in IE?

2. In the returned URL, I want to check for something specific in the string.
- IF the following URLs were returned:
or www.domain.com/sound.php?level.php?beats=hard&mute=1

Then I want to do something like:
if(product == "icecream") { }
if(beats== "hard") { }
import flash.external.ExternalInterface;
var urlPath;

function geturlhttp() {
urlPath = ExternalInterface.call("window.location.href.toString");
//Here I assign the url to a text box on the stage
_root.urlText.text = urlPath;

Open in new window

Question by:mrsam
  • 3
  • 2
LVL 14

Expert Comment

ID: 26160400
Actually you shouldn't do it like this. First off, the URL you showed me contains a query string, therefore you shouldn't get it from the swf because the swf shouldn't be using it, it's used for the page, not for the swf. Furthermore, even if you want to do it, you should pass the variables to the swf instead and not use the swf to parse the URL and get the variables.

The right way to do it is to pass the variables you want the swf is using FlashVars. Then check the FlashVars using the swf.

The only time you want to be using the url is getting the url of the swf and not the page. Or just the location where the swf is embedded.

For example in AS2,
This will return the swf's url

This gets the location:
trace(_url.substr(0, _url.lastIndexOf("/")));

In AS3,
trace(loaderInfo.url.substr(0, loaderInfo.url.lastIndexOf("/")));

However, this won't be what you are needing anyway, just to tell you about them in the future in case you need them. In the future, when you ask an Actionscript question, you can't expect experts to guess your Actionscript version. Furthermore, your code works for both AS2 and AS3, making me puzzled on whether you are using AS2 or AS3. Unless your code can be seen obviously as AS2 or AS3, please specify.

Below is an example of what you should do:

<script type="text/javascript">

var flashvars = {
  cat: 1,
  hex: 2234515,
  product: icecream

swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars);


Then in AS2, you will do this

if (product == "icecream") {

In AS3, you will do this

if (loaderInfo.parameters.product == "icecream") {

Author Comment

ID: 26161221
Hi TanLiHao, thansk a lot for your response!

Im really sorry that I wasn't being specific enough - guess it was late and I still hung over from new years day :)

I see what you're saying with the swfobject and javascript - my problem is that, I can't add anything to the page itself. I'm running this in a banner, which will run on various sites, that I have no control of.
So I need to find a method where I can check what the URL of the browser is, directly from my SWF.
I can surely use your example in cases where I can add code to the page itself, so i'll certainly write it down :)

Really sorry again for not being specific enough in my first post, i'll make sure it won't happen again!
Btw im using AS2.
LVL 14

Accepted Solution

TanLiHao earned 2000 total points
ID: 26164924
Hmm, that's bad that you can't control the page.
However, your code should already work.

For example, go to this example page: http://www.freewebtown.com/tanlihao/SampleQueryStringPage.html?cat=1&hex=2234515&product=icecream

It works.

The code used is this:

import flash.external.ExternalInterface;
currentURL.text = ExternalInterface.call("window.location.href.toString");
queryString.text = ExternalInterface.call("window.location.search.substring", 1);

Yes I tested this on IE.

Author Comment

ID: 26166952
What does the "1" at the end mean?

But got it working. Ive posted the complete solution below.

import flash.external.ExternalInterface;
currentURL.text = ExternalInterface.call("window.location.href.toString");

urlContainer = ExternalInterface.call("window.location.search.substring", 1);
queryString.text = urlContainer;

var vari:LoadVars = new LoadVars(); 
if(vari.product == "icecream"){

Open in new window

LVL 14

Expert Comment

ID: 26168683
This is like calling a javascript function with arguments.

For example, the code actually means this


This substring the text get from search from index 1 to the end, thus taking away the "?" character.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to record live broadcast.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question