flashvars 不适用于从 url 读取的字母数字值
我的 flashvars 有问题,当我从浏览器读取 url 时,如果我只为 Id (我的 url 中的参数)分配了数字,则一切正常,但如果我的 id 包含字符,则它不起作用,我不想要更改 mxml 文件中 flash 端的任何内容,我想用 javascript 来完成
这里是我的代码
<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>
,稍后在代码中我有
flashvars.StartPage = getQueryVariable("Id");
swfobject.embedSWF(
"myFlashFile.swf", "flashContent",
"100%", "100%",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
一个不起作用的示例
myUrl/default.aspx?Id=0061A
而
myUrl/default.aspx?Id=0061 完美运作
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Flash 将 flashvars 作为字符串接收,如果它对字符串不起作用,那么它必须将它们转换为数字或以其他方式解析它们 - 改变 JavaScript 方面不会改变任何东西,无论你怎么做。然而,你绝对可以改进你在 JavaScript 中所做的事情(谁知道,也许你以后会需要它?)
因此, return 不是一个函数,因此
return (pair[1])
是多余的,应该是返回pair[1]
。然而,从函数中间返回是一种不好的风格。更糟糕的是,您的函数将返回未定义或字符串 - 良好的风格是一致的,并在每种情况下返回相同的类型。通过生成大量数组来解析字符串是一个坏主意 - 您不需要这样做,而是使用正则表达式。例如,类似这样的东西应该可以完成这项工作:/([^&=]+)=([^&]*)/g
一个好处是正则表达式比正则表达式快得多JavaScript 代码的其余部分,因为它们是用 C 实现的。函数内部块内的变量被“提升” - 这意味着varpair
实际上是在循环外部声明的。我认为从技术上在实际声明的地方编写变量声明是一种很好的做法 - 这有助于以后避免某些陷阱。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 bereturn 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 thatvar 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.