Javascript variables in ASP.NET (screen resolution)

S

srini

Frens,

I am trying to get screen resolution of the client machine using
javascript and use those values in my project. I looked at some of the
examples given in this group, i tried to implement the same but
everytime i get nulls, i tested javascript by keeping alert statements
and everything looks good to me....
I am trying to figure it out what peice of info is missing in this
code.....
Here is my code

In PageLoad....
===========
If (Not IsPostBack) Then
RegisterStartupScript("MyScript", _
"<script language=javascript>" & _
"document.forms['Form1'].submit();</script>")
Else
Dim resW As String = Request.Form("resW")
Dim resH As String = Request.Form("resH")
End If

In ASPX page
==========
<HTML>
<HEAD>
<script language="javascript">
document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;
</script>

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT id="resW" type="hidden" name="resW" runat="server">
<INPUT id="resH" type="hidden" name="resH" runat="server">
</form>
</body>
</HTML>

I looked at some examples posted in this site...saying that they are
able to get those values
<http://groups.google.com/group/micr...ues+in+ASP.NET&rnum=11&hl=en#19a7705d4458e6e6>

Thanks in advance...

Thanks
Srini
 
M

Mark Rae

document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;

document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;
 
B

bruce barker

getElementsByName returns an array (as names can be dup'd), so you want:


<script language="javascript">
document.getElementsByName('resW')[0].value = screen.width;
document.getElementsByName('resH')[0].value = screen.height;
</script>

-- bruce (sqlwork.com)
 
S

srini

Hi Mark,

This change wahtever u suggested does not seems to work...if we have
the "name" and "id" for any form hidden element, Only the "name" will
be taken care...in our case id will not work.

You can copy the code which i mentioned in the previous mail..and try
it..is there any workaround for this problem

Thanks
Srini
 
S

srini

Hi Bruce,

This does not seems to work. I already tried this way..is there
anyother possible workaround...

Thanks
Srini


bruce said:
getElementsByName returns an array (as names can be dup'd), so you want:


<script language="javascript">
document.getElementsByName('resW')[0].value = screen.width;
document.getElementsByName('resH')[0].value = screen.height;
</script>

-- bruce (sqlwork.com)

Frens,

I am trying to get screen resolution of the client machine using
javascript and use those values in my project. I looked at some of the
examples given in this group, i tried to implement the same but
everytime i get nulls, i tested javascript by keeping alert statements
and everything looks good to me....
I am trying to figure it out what peice of info is missing in this
code.....
Here is my code

In PageLoad....
===========
If (Not IsPostBack) Then
RegisterStartupScript("MyScript", _
"<script language=javascript>" & _
"document.forms['Form1'].submit();</script>")
Else
Dim resW As String = Request.Form("resW")
Dim resH As String = Request.Form("resH")
End If

In ASPX page
==========
<HTML>
<HEAD>
<script language="javascript">
document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;
</script>

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT id="resW" type="hidden" name="resW" runat="server">
<INPUT id="resH" type="hidden" name="resH" runat="server">
</form>
</body>
</HTML>

I looked at some examples posted in this site...saying that they are
able to get those values
<http://groups.google.com/group/micr...ues+in+ASP.NET&rnum=11&hl=en#19a7705d4458e6e6>

Thanks in advance...

Thanks
Srini
 
M

Mark Rae

This change wahtever u suggested does not seems to work...if we have
the "name" and "id" for any form hidden element, Only the "name" will
be taken care...in our case id will not work.

That makes no sense at all...
You can copy the code which i mentioned in the previous mail..and try
it..

Works perfectly for me...
is there any workaround for this problem

I have no idea...
 
S

srini

Hi Mark,

I tried with the change you suggested..but what iam getting is

document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;

Javascript error .......is not null or not an object..

Let me keep my code once again so that you can know what exactly iam
doing ..I am using ASP 1.1

In PageLoad
=========
Protected WithEvents resW As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents resH As
System.Web.UI.HtmlControls.HtmlInputHidden

If (Not IsPostBack) Then
RegisterStartupScript("MyScript", _
"<script language=javascript>" & _
"document.forms['Form1'].submit();</script>")
Else
Dim resW As String = Request.Form("resW")
Dim resH As String = Request.Form("resH")
End If

ASPX Page
=========
<HTML>
<HEAD>
<title>first</title>
<script language="javascript">
document.getElementByName('resW').value = screen.width
document.getElementByName('resH').value = screen.height
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT id="resW" type="hidden" name="resW" runat="server">
<INPUT id="resH" type="hidden" name="resH" runat="server">
</form>
</body>
</HTML>

What the values i get from resW and resH is nulls....

Thanks
Srini
 
M

Mark Rae

I tried with the change you suggested..but what iam getting is

document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;

Javascript error .......is not null or not an object..

OK - which *specific* line of the JavaScript causes the error...?
<script language="javascript">

Firstly, that is not XHTML-compliant - instead, you should be using

<script type="text/javascript">

However, I doubt very much that that is causing your problem.
document.getElementByName('resW').value = screen.width
document.getElementByName('resH').value = screen.height

Secondly, as has already been pointed out to you, that is *never* going to
work! The document object does *not* have a method called
"getElementByName" - I don't know how you imagine that it does... It has
methods called "getElementById" and "getElementsByName" - notice the "s" in
getElement*s*ByName.

http://www.google.co.uk/search?sour...ls=GGLG,GGLG:2006-28,GGLG:en&q=getElementById
http://www.google.co.uk/search?sour...=GGLG,GGLG:2006-28,GGLG:en&q=getElementByName
http://www.google.co.uk/search?hl=en&rls=GGLG,GGLG:2006-28,GGLG:en&q=getElementsByName&meta=

Also, what browser are you using to test your code? "screen.width" and
"screen.height" will not work in all browsers
http://chattyfig.figleaf.com/pipermail/flashcoders/2001-May/000103.html
 
S

srini

Hi Mark,

Thanks for your reply...

#1 OK - which *specific* line of the JavaScript causes the error...?
document.getElementById('resW').value = screen.width;

Javascript error.. document.getElementById(' ') is not null or not an
object..

#2. document.getElementByName('resW').value = screen.width
This is typo....actually i declared in the program correctly

#3. Also, what browser are you using to test your code? "screen.width"
Iam using IE6.0

am i missing something ???..this is driving me crazy....

Thanks
Srini
 
M

Mark Rae

#1 OK - which *specific* line of the JavaScript causes the error...?
document.getElementById('resW').value = screen.width;

Javascript error.. document.getElementById(' ') is not null or not an
object..

Er, well there's your problem right there!

document.getElementById(' ')

Unless this is another typo, the JavaScript is looking for a control whose
id is a space character, not resW or resH.

Do a View Source on the page...
 
S

srini

Hi Mark,

Thanks for your reply...I got the result but this time i did it in
different way.

In Page Load event
==============
Dim popupScript As String = "<script
language=javascript>document.forms(0).action='second.aspx?sWdHt=' +
screen.width + ',' + screen.height
;document.forms(0).submit();</script>"

If (Not IsPostBack) Then
RegisterStartupScript("MyScript", popupScript)
Else
Dim test as string = Request("sWdHt")
End If

NO ASPX changes

Now i could see the value in the string "test" . Appreciate your help
and it helped me lot in exploring new things.

Thanks
Srini
 
M

Mark Rae

That's great.

You should still read up on XHTML-compliance, though, as your JavaScript is
quite non-compliant and *extremely* IE-specific...

<script language=javascript>
document.forms(0)

etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top