参数传递给JS方法高效

时间:2013-03-13 06:00:25

标签: javascript

有人请告诉我这个片段有什么问题吗?

<!DOCTYPE html>
<html>
<head>
<script>
function anotherWindow(msg, myWidth, myHeight)
{
 theWindow=window.open('','','width=myWidth,height=myHeight');
 theWindow.document.write(msg);
 theWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="One more window" onclick="anotherWindow('Here is the  window',200,100)" />

</body>
</html>

当第一个参数(msg)成功传递给方法.write时,方法.open中与窗口大小相关的两个参数没有带来任何结果 - 该方法坚持某些默认值。

我对变量传递的理解有什么问题?

2 个答案:

答案 0 :(得分:2)

正在以正确的方式传递论据,但它们并非used正确的方式。

theWindow=window.open('','','width=myWidth,height=myHeight');

引号中有myWidthmyHeight,它们不会告诉javascript这些是变量。这两个必须在引号之外。

像这样:

theWindow=window.open('','','width='+myWidth+',height='+myHeight);

答案 1 :(得分:2)

您需要实际替换变量的值。

theWindow=window.open('','','width='+myWidth+',height='+myHeight);