0

A javascript function is being called at the time of page loading which is returning multiple values and i need to receive those values. What should i specify in form or body tag so as to make use of those values in my jsp page?

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. How should i receive those values of myWidth and myHeight so as to use them in the same jsp page?

0

4 Answers 4

1

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;
//...
Sign up to request clarification or add additional context in comments.

Comments

0

Your commented out return statement is fine:

return {myWidth:myWidth, myHeight:myHeight};

It can then be used in other javascript like so:

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.

Comments

0

Return a JSON object or an array of results.

Comments

0

You can pass all your variables together, using some JSON notation to pass the data

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

Once you have the data, decode the JSON and use your variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.