how i get the width of browser using

HOUSSEM MAHJOUBI 286 Reputation points

Hi members

i want get the width of the web browser and i tried without succeed.

this is my try :

Dim wid = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width

Dim hy = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

this code doesn't work in server side it work fine when i use visuel studio

i tried another code using javascript and the width i can't call it in like the variable is empty has no value

this is the code :

 <script type="text/javascript">
function SetName() {
    var label = document.getElementById("<%=Label24.ClientID %>");
    //Set the value of Label.
    label.innerHTML = screen.width;
    //Set the value of Label in Hidden Field.
    document.getElementById("<%=Label26.ClientID %>").value = label.innerHTML;

and this is when i use that :

 Protected Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click

        Label24.Text = (Request.Form(Label26.UniqueID))

    End Sub

the label24.text has no value i have no idea why the value didn't pass.

please help

A set of technologies in the .NET Framework for building web applications and XML web services.
3,366 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
911 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,632 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 27,796 Reputation points Microsoft Vendor


    You can try the following methods:

    <input type="hidden" value="" name="clientScreenWidth" id="clientScreenWidth" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
         <script src="Scripts/jquery-3.4.1.min.js"></script>
            $(document).ready(function () {
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim width As String = HttpContext.Current.Request.Params("clientScreenWidth")
            If width IsNot Nothing Then
            End If
        End Sub

    User's image

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

2 additional answers

Sort by: Most helpful
  1. Viorel 113.8K Reputation points

    Show more details about Label26. I think that you should use something like this:

    <asp:HiddenField id="Label26" runat="server"/>

    Then try this: Dim width As String = Label26.Value. Check if width contains the value obtained by JavaScript.

  2. Bruce ( 59,056 Reputation points

    first screen.width is the monitor size, not the browser. you probably want window.innerWidth. second as the user can resize the browser, its of little use to the server. you should use responsive design.