3

Now that i learned how to pass values to an SWF object via flashvars, could you please guide me how can i pass values from a querystring to javascript?

What do i mean? In the following example i hard-code the xml file to load in the SWF object.

<script type="text/javascript">
    var so = new SWFObject("preview.swf", "", "100%", "100%", "9", "#ffffff");
    so.addParam("allowFullScreen", "true");
    so.addParam("scale", "noscale");
    so.addParam("menu", "false");
    so.addVariable("xmlPath", "xml/exampleData.xml");
    so.write("flashcontent");
</script>

Since the Xml file is created dynamic, the xml should be loaded from the value of a query-string. (I guess).

Supposing my url is http://www.example.com/load.aspx?XmlFile=SomeData

How can i pass it to the javascript side? Like..

  so.addVariable("xmlPath", "xml/<% SomeData %>.xml");

or whatever it needs to make it work.

UPDATE: Besides the above example, is there any way of creating the JavaScript, in server-side?

Community
  • 1
  • 1
OrElse
  • 9,709
  • 39
  • 140
  • 253

3 Answers3

3

Try something like:

function GetQueryString(param) 
{
 var url = window.location.search.substring(1);
    var params = url.split("&");
    for (i=0;i<params.length;i++) 
    {
        var p = params[i].split("=");
        if (p[0] == param) 
        {
            return p[1];
        }
    }   
}

And use it like:

so.addVariable("xmlPath", "xml/" + GetQueryString("XmlFile") + ".xml");
KMån
  • 9,896
  • 2
  • 31
  • 41
0

window.location.href contains the querystring of the current page, this should work:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
0

function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for(var i = 0; i < hashes.length; i++)
{
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}

return vars;

}

user496879
  • 11
  • 1
  • 2
    Its always good to provide a source: [http://snipplr.com/view/799/get-url-variables/](http://snipplr.com/view/799/get-url-variables/) – KMån Nov 04 '10 at 08:43