473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3164
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jay | last post by:
Hi everybody !! I am just wondering if Javascript and PHP can interact with each other ? Let's say i have a variable in Javascript ( call it "var x = 6;") Is there anyway that we can use "x"...
2
by: Ang Talunin | last post by:
Hey, How to do something like this: <head> <style> img { width: <script>blaat();</script>;} </style> <script> function blaat()
8
by: Victor | last post by:
I need a JavaScript timer - I have five events I need to time, that can be triggered by a mouseclick event, or a keypress event. Each event is separated by only one to two seconds. The first...
3
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Keep in mind that i am not a javascript programmer but php. Here is...
14
by: Seige | last post by:
Ever had headache when you can't resize the background width using CSS: body{ background: url(/images/bg.jpg) ; background-width: 800px; } It won't work, would it? Of course not, it's not even...
7
by: Norman Swartz | last post by:
I want to place some graphic images on the web that are optimally viewed at a resolution of 1024 by 768 pixels. Is it possible, within Javascript,to force a particular screen resolution?
19
by: mantrid | last post by:
I have the following link to bring up an alert with a figure in it based on the screen resolution. but instead of displaying a number 'NaN' is displayed. <a href="javascript:alert('Your ideal...
0
by: sirumalla.srinivas | last post by:
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 .net groups, i tried to implement...
7
crystal2005
by: crystal2005 | last post by:
Hi all, I'm currenty creating a website. I got one confusing problem related to CSS and JavaScript actually, not really html. The problem is, I actually want my welcome screen page is to be random...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.