Javascript variables in ASP.NET (screen resolution)

  • Thread starter Thread starter srini
  • Start date Start date
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
 
document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;

document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;
 
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)
 
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
 
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
 
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...
 
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
 
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
 
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
 
#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...
 
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
 
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
 
Back
Top