简体   繁体   English

如何从JavaScript函数接收多个值?

[英]How to receive multiple values from a javascript function?

A javascript function is being called at the time of page loading which is returning multiple values and i need to receive those values. 页面加载时正在调用javascript函数,该函数返回多个值,我需要接收这些值。 What should i specify in form or body tag so as to make use of those values in my jsp page? 我应该在form或body标签中指定什么,以便在jsp页面中使用这些值?

The code i am using is: 我使用的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function Browser()
{
    var myWidth;
    var myHeight;

    if( typeof( window.innerWidth ) == 'number' ) { 

    //Non-IE 

    myWidth = window.innerWidth;
    myHeight = window.innerHeight; 

    } else if( document.documentElement && 

    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 

    //IE 6+ in 'standards compliant mode' 

    myWidth = document.documentElement.clientWidth; 
    myHeight = document.documentElement.clientHeight; 

    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

    //IE 4 compatible 

    myWidth = document.body.clientWidth; 
    myHeight = document.body.clientHeight; 

    } 
    return {myWidth:myWidth, myHeight:myHeight};

    }
</script>
</head>
<body onload="Browser()">
<form name="hello" action="" method="post" >
<table align="center" border="1">
<tr>
<td>
User Name:
</td>
<td>
<input type="text" name="user"/>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="pwd"/>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>

In the above code function Browser is returning the resolution values and i had called that function using onload() method. 在上面的代码函数中,浏览器返回了分辨率值,我已经使用onload()方法调用了该函数。 How should i receive those values of myWidth and myHeight so as to use them in the same jsp page? 我应该如何接收这些myWidth和myHeight值,以便在同一jsp页面中使用它们?

you can return a object containing all the relevant values: 您可以返回包含所有相关值的对象:

function myFunction() {
    return { val1: 1, val2: 2, val3: 3 };
}

usage: 用法:

var tmp = myFunction();
var val1 = tmp.val1;
var val2 = tmp.val2;
//...

Your commented out return statement is fine: 您注释掉的退货声明很好:

return {myWidth:myWidth, myHeight:myHeight};

It can then be used in other javascript like so: 然后可以在其他javascript中使用它,如下所示:

var dimension = Browser();
var width = browser.myWidth;
var height = browser.myHeight;

You will not have access to these variables on the server (unless you post them) since javascript executes on the client. 由于将在客户端执行javascript,因此您将无法访问服务器上的这些变量(除非您将其发布)。

返回JSON对象或结果数组。

You can pass all your variables together, using some JSON notation to pass the data 您可以将所有变量一起传递,使用一些JSON表示法传递数据

document.write('{myWidth:'+myWidth+',myHeight:'+myHeight+'}');

Once you have the data, decode the JSON and use your variables. 获得数据后,解码JSON并使用变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM