471,595 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Javascript variables in ASP.NET (screen resolution)

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/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/7e8a1109b11b83a3/19a7705d4458e6e6?lnk=st&q=getting+Javascript+value s+in+ASP.NET&rnum=11&hl=en#19a7705d4458e6e6>

Thanks in advance...

Thanks
Srini

Dec 27 '06 #1
11 3056
"srini" <si****************@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;
document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;
Dec 27 '06 #2
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)
srini wrote:
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/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/7e8a1109b11b83a3/19a7705d4458e6e6?lnk=st&q=getting+Javascript+value s+in+ASP.NET&rnum=11&hl=en#19a7705d4458e6e6>

Thanks in advance...

Thanks
Srini
Dec 27 '06 #3
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
Mark Rae wrote:
"srini" <si****************@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
document.getElementsByName('resW').value = screen.width;
document.getElementsByName('resH').value = screen.height;

document.getElementById('resW').value = screen.width;
document.getElementById('resH').value = screen.height;
Dec 27 '06 #4
Hi Bruce,

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

Thanks
Srini
bruce barker wrote:
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)
srini wrote:
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/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/7e8a1109b11b83a3/19a7705d4458e6e6?lnk=st&q=getting+Javascript+value s+in+ASP.NET&rnum=11&hl=en#19a7705d4458e6e6>

Thanks in advance...

Thanks
Srini
Dec 27 '06 #5
"srini" <si****************@gmail.comwrote in message
news:11**********************@h40g2000cwb.googlegr oups.com...
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...
Dec 27 '06 #6
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

On Dec 27, 10:23 am, "Mark Rae" <m...@markNOSPAMrae.comwrote:
"srini" <sirumalla.srini...@gmail.comwrote in messagenews:11**********************@h40g2000cwb.g ooglegroups.com...
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 problemI have no idea...
Dec 27 '06 #7
"srini" <si****************@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
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?sourc...getElementById
http://www.google.co.uk/search?sourc...tElementByName
http://www.google.co.uk/search?hl=en...tsByName&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/piperma...ay/000103.html
Dec 27 '06 #8
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

Mark Rae wrote:
"srini" <si****************@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
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?sourc...getElementById
http://www.google.co.uk/search?sourc...tElementByName
http://www.google.co.uk/search?hl=en...tsByName&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/piperma...ay/000103.html
Dec 28 '06 #9
"srini" <si****************@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
#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...
Dec 28 '06 #10
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='seco nd.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

On Dec 27, 4:38 pm, "Mark Rae" <m...@markNOSPAMrae.comwrote:
"srini" <sirumalla.srini...@gmail.comwrote in messagenews:11**********************@73g2000cwn.go oglegroups.com...
#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...
Dec 28 '06 #11
"srini" <si****************@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...

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
Dec 28 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jay | last post: by
2 posts views Thread by Ang Talunin | last post: by
8 posts views Thread by Victor | last post: by
3 posts views Thread by mbasil7 | last post: by
19 posts views Thread by mantrid | last post: by
reply views Thread by sirumalla.srinivas | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.