1

I have a problem with flashvars , when I read the url from browser if I have assigned just numbers to Id (my paramter in url) ,everything works fine, but if my id includes character, then it does not work , I don't want to change anything in flash side in mxml files, I want to do it with javascript

here is my code

   <script type="text/javascript">
   function getQueryVariable(variable) 
   {
    var query = window.location.search.substring(1);  
    var vars = query.split("&");  
    for (var i=0; i<vars.length; i++) 
    {    
        var pair = vars[i].split("="); 
        if (pair[0] == variable)
            return (pair[1]);
    }
}
  </script>  

and later in the code I have

    flashvars.StartPage = getQueryVariable("Id"); 

     swfobject.embedSWF(
            "myFlashFile.swf", "flashContent", 
            "100%", "100%", 
            swfVersionStr, xiSwfUrlStr, 
            flashvars, params, attributes);

an example of what does not work is

myUrl/default.aspx?Id=0061A

whereas

myUrl/default.aspx?Id=0061 works perfectly

Mahsa
  • 115
  • 2
  • 11

1 Answers1

0

Flash receives flashvars as strings, if it doesn't work for strings, then it must be casting them to numbers or parsing them in some other way - changing the JavaScript side won't change anything, no matter how you do it. However, you can definitely improve on what you do in JavaScript (who knows, maybe you will need it some other time?)

So, return is not a function, thus return (pair[1]) is redundant, should be return pair[1]. However, returning from the middle of the function is a bad style. Worse yet your function will return either undefined or string - a good style is being consistent and return the same type in every situation. It is a bad idea to parse string by generating lots of arrays - you don't need to do that, use regular expressions instead. For example, something like this should do the job: /([^&=]+)=([^&]*)/g a bonus is that regular expressions are way much faster then the rest of JavaScript code because they are implemented in C. Variables inside blocks inside function are "elevated" - this means that var pair is actually declared outside the loop. I'd call it a good practice to technically write the variable declaration in the place it actually is declared - this helps afterwards to avoid certain pitfalls.

  • thanks for the help, but I even tried not using this fucntion and just made it simpler using flashvars.StartPage = swfobject.getQueryParamValue("Id"), but still the same problem , works with number but not combination of numbers and string – Mahsa Feb 10 '12 at 20:21