js undefined error
we have the following widget code : index.html
<script type="text/javascript" language="javascript">
//<![CDATA[
var param1 = "value";
var param2 = "value2";
//]]>
</script>
<script type开发者_JAVA百科="text/javascript" src="http://example.com/js/widget.js"></script>
widget.js
var param1;
var param2;
document.write('<iframe '
+ ' allowtransparency="true"'
+ ' src="http://example.com/html/'
+ '?param1=' + param1
+ '¶m2=' + param2
+ '">'
+ '</iframe>');
but widget.js L6 param1== undefined and param2== undefined why? param1 is dead at widget.js L1?
you must remove variable definitions in widget.js, var keyword defines new variable with value of undefined.
var param1; //remove this
var param2; //remove this
document.write('<iframe '
+ ' allowtransparency="true"'
+ ' src="http://example.com/html/'
+ '?param1=' + param1
+ '¶m2=' + param2
+ '">'
+ '</iframe>');
I'm going to hazard a guess and suggest that that isn't the complete widget.js, and that the code you have provided is inside a function call.
The first two lines of the widget.js code you have posted will create new versions of param1/2 in the local scope and they will be undefined.
Remove those lines.
精彩评论