Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:54:22+00:00 2026-06-01T07:54:22+00:00

This is the JS to detect the screen resolution on my page naming index.html

  • 0

This is the JS to detect the screen resolution on my page naming index.html and sending it to php, so that the values can be retrived using $_GET:-

<script type="text/javascript">

width = screen.availwidth;
height = screen.availheight;

if (width > 0 && height >0) {
    window.location.href = "http://localhost/main.php?width=" + width + "&height=" + height;
} else 
    exit();

</script>

This is the content of my PHP file naming process.php:-

<?php
$screen_width = $_GET['width'];
$screen_height = $_GET['height'];

//EDITED PORTION
echo '<style>#Wrapper {width:'.$screen_width.';}</style>';
?>

Now after getting the screen size i want to display the index.php.

NOTE THESE STEPS, AS TO UNDERSTAND THE WAY I WANT THINGS TO WORK:-

  1. Page Load Started
  2. Javascript detected available screen width.
  3. It passes it to php file and the PHP variable got the screen available screen width.
  4. Then page get displayed using that php variable (Means the page width is being set using that php variable)

In the whole process the index.php should not be reloaded. So, How it can be done?

EDIT:

As the JS passes the value to the php file it gets loaded, what i want is to get the screen resolution and accordingly set the website page width. But if JS send the data to php file and if at all we get it through ajax, it may can happen that my website get fully loaded and the style would not be implemented because it has already been loaded all the stuffs before the ajax work.

Is there any method which is efficient enough or we can do this one in a way that it become efficient.

This is my EDIT:2

To be more clear i’ll actually explain what i want to achieve using the following image:-

Actually i want to implement things like this

Now if ill get the screen resolution say 800px then i know that i want to deduct 200px of left side from it, so it will become 600px and as my divs are 250px i’ll be able to add only two. Therefore, to avoid showing that 100px in right i’ll adjust my website page width to 700px.

Things i can do in php, but how to get all things get done at same time on same page?

The whole area you can call as #Wrapper
Right One as #SideBar
Left Area as #LeftContent

Hope i have explained things well. So please if someone can help me doing the needful 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:54:24+00:00Added an answer on June 1, 2026 at 7:54 am

    I’m not sure if I understood your problem but this is what I’ve got: if you only intend to get the screen width and then set the class I disencourage you to do it that way. It’s not nice and in the future if you need to change your page layout you’ll be doomed by having all those echo in php and you’ll avoid a lot of redirects (index.html -> process.php -> index.php).

    By your example you can do it all in Javascript.

    This is example is with JQuery

    var width = $(window).width(); //get the width of the screen
    $('#Wrapper').css("width",width); //set the width to the class
    

    If the idea is for very different screen resolutions (from PCs to SmartPhones), you should consider changing your approach.

    Good luck 🙂


    TRY THIS: (I believe is what you want)

    You can do this with just JS and CSS.
    Here is an example:

    set the style for your body margin and your test div:

    <style type="text/css">
        body {
            margin: 0px 0px 0px 200px;
    }
        .divForTesting {
            width: 250px;
            border: 1px solid green;
            height: 100px;
            float: left;
    }
    </style>
    

    then add this script:

    <script>
        // get the screen width
        var width = $(window).width();
        // get the number of pixels left in the screen (which is the width
        // minus the margin you want, rest by 250px which is the "inner boxes" width)
        var size = (width - 200) % 250;
        // then re-set the margin (the "-10" is because you'll need extra space
        // for borders and so on)
        $('body').css("margin-left",200+(size-10));
    </script>
    

    and test it with this html:

    <!-- I've aligned the outter div to the right -->
    <div style="border: 1px solid red; float:right;" id="test">
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    <div class="divForTesting">test</div>
    </div>
    

    It works in different resolutions.

    Give it a try 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that in Safari on an iPhone you can detect the screen's orientation
I am using the following code to detect a users screen resolution upon load
I wonder if anyone can work this HTML, CSS, Javascript puzzle out? I'm using
I want to detect users' screen size and pass this into a charting application
I am using this code for detect the source language.. jsonObj = new JSONObject(response);
So we have a photo like this (source: vkontakte.ru ) How to detect that
How can you detect the time zone in Silverlight? This is an out of
For some reason this basic JS to detect the browser's resolution works in Safari
How could I detect an image on the screen and then act on that
Can anybody recommend a good way to detect screen width on a HTML5 Android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.