Read URL from browser

Posted on 2010-01-01
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:

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

var urlPath;

function geturlhttp() {

urlPath ="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
    LVL 14

    Expert Comment

    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

    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

    Hmm, that's bad that you can't control the page.
    However, your code should already work.

    For example, go to this example page:

    It works.

    The code used is this:

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

    Yes I tested this on IE.

    Author Comment

    What does the "1" at the end mean?

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

    import flash.external.ExternalInterface;
    currentURL.text ="window.location.href.toString");
    urlContainer ="", 1);
    queryString.text = urlContainer;
    var vari:LoadVars = new LoadVars(); 
    if(vari.product == "icecream"){

    Open in new window

    LVL 14

    Expert Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    AWAY 3d Using BitMapCubeTexture 1 386
    Action Script commands 4 300
    Flash movie or gif/animation? 3 813
    YouTube video issue 28 85
    Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
    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 shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now