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?

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

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;

}

1 Comment

Its always good to provide a source: http://snipplr.com/view/799/get-url-variables/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.