Forum Moderators: open

Message Too Old, No Replies

Run codes if and only if screen width is higher than or equal to 1024

         

irock

11:07 pm on Aug 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I'm not very familiar with Javascript, but I have a simple question regarding screen width retrieval.

Say, if I want to run certain codes whenever the available screen width is higher than or equal to 1024, do I simply throw this into my HTML?

<script type="text/javascript">

if (screen.width >= 1024) {
var mytext = "Hello again";
document.write(mytext);
}

</style>

proper_bo

9:03 am on Aug 12, 2004 (gmt 0)

10+ Year Member



Hi there,

can't answer the question but would like to add my request as it is very similar.

I would like to include a banner on the right of my page if the screen res is over 800x600. As you can tell my request can therefore be sorted with an answer to the question posted by irock.

Thanks in advance.

irock

9:22 am on Aug 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, I found my own solution.

<script type="text/javascript">
if (document.body.clientWidth >= 800) {
var banner = '<div style="position:absolute; right:0px; top:300;">

{ Your banner here }

</div>';
document.write(banner);
}
</script>

This works so great b/c it even detects the actual window width rather than the screen width. If document.body.clientwidth is replaced by screen.width, then the banner could have overlapped the content at 800x600 due to window resizing by the user.

proper_bo

9:27 am on Aug 12, 2004 (gmt 0)

10+ Year Member



Good work.

Does this mean the banner will dis/appear when the user increases or decreases the size of the window without closing and reopening?

Guess I'll go try this out. Thanks.

Lord Majestic

9:28 am on Aug 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works so great b/c it even detects the actual window width rather than the screen width

What one should take into consideration is that the most popular browser - IE might open windows that are not maximised, in fact some users prefer non-maximised windows for their browsing experience. Given that you definately want to be careful as not to give impression that there is nothing on non-maximised page because people will just close it.

While width of 1024 or higher does sound pretty reasonable, it might be good idea to still show what you've got on screen as people don't change browser resolutions every 5 seconds (diff resolutions in games is probably the only frequent change I do) - chances are what they've got is what they will keep.

irock

9:33 am on Aug 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lord Majestic,

That's true. But on my site, the banner is quite large... and I don't want the banner to overlay my content and result in annoying my visitors. I would rather scarifice some ads impressions for some usability.