Javascript - Run my script only if landscape is detectedCreate GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Which creature is depicted in this Xanathar's Guide illustration?

The difference between きわめて and いたって

What was Varys trying to do at the beginning of S08E05?

Why doesn't Iron Man's action affect this person in Endgame?

How do I know which cipher suites can be disabled?

Why when I add jam to my tea it stops producing thin "membrane" on top?

Proper way to use apply_filters() with class functions?

What color to choose as "danger" if the main color of my app is red

Why are goodwill impairments on the statement of cash-flows of GE?

Will the volt, ampere, ohm or other electrical units change on May 20th, 2019?

tikz drawing rectangle discretized with triangle lattices and its centroids

Will British Airways compensate if my entertainment screen was defective for an entire flight?

Were any toxic metals used in the International Space Station?

Can my American children re-enter the USA by International flight with a passport card? Being that their passport book has expired

Who commanded or executed this action in Game of Thrones S8E5?

Does addError() work outside of triggers?

Why do galaxies collide

When is the AS keyword required?

Does the wearer know what items are in which patch in the Robe of Useful items?

Can anyone give me examples of the relative-determinative 'which'?

Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?

Is there an academic word that means "to split hairs over"?

Could there be something like aerobatic smoke trails in the vacuum of space?

Do crew rest seats count towards the maximum allowed number of seats per flight attendant?



Javascript - Run my script only if landscape is detected


Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I am running a script to show a notification within a menu with scroll, but I do not know how to detect if the device has orientation landscape to validate the script.



The call onClick="VerHayMas();" works perfectly, but if the user open the menu once, clicking on #boton-menu and with your device in portrait, after changing the orientation to landscape the script no longer meet the objective.




The script has its logic ONLY if the device is in landscape, which is
when the menu needs to show the notification.




So, is it possible that my script is only valid with (max-width:999px) and (orientation:landscape), ignoring the portrait...?



I am a beginner in JS, and I do not know how to do it, really.



Any idea?



Thanks in advance!



HTML & CSS



#mas-menu display:none

<div id="boton-menu" onClick="VerHayMas();">+</div>


Script:



var clicksVerHayMas = 0;

function VerHayMas()
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);

;


EDIT:



I have tried with the following script, but it does not work. If the user makes a call to onClick="VerHayMas();" in portrait mode, the script is no longer running in landscape mode.



What am I doing wrong here?



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)

var clicksVerHayMas = 0;
function VerHayMas()
clicksVerHayMas = clicksVerHayMas +1;
if(clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
,4000);
;










share|improve this question



















  • 1





    You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

    – Alicia
    May 4 at 0:21











  • If you simply want to hide something on landscape orientation, why not use one line of CSS?

    – Salman A
    May 4 at 12:58











  • Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

    – Bergi
    May 4 at 13:07

















7















I am running a script to show a notification within a menu with scroll, but I do not know how to detect if the device has orientation landscape to validate the script.



The call onClick="VerHayMas();" works perfectly, but if the user open the menu once, clicking on #boton-menu and with your device in portrait, after changing the orientation to landscape the script no longer meet the objective.




The script has its logic ONLY if the device is in landscape, which is
when the menu needs to show the notification.




So, is it possible that my script is only valid with (max-width:999px) and (orientation:landscape), ignoring the portrait...?



I am a beginner in JS, and I do not know how to do it, really.



Any idea?



Thanks in advance!



HTML & CSS



#mas-menu display:none

<div id="boton-menu" onClick="VerHayMas();">+</div>


Script:



var clicksVerHayMas = 0;

function VerHayMas()
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);

;


EDIT:



I have tried with the following script, but it does not work. If the user makes a call to onClick="VerHayMas();" in portrait mode, the script is no longer running in landscape mode.



What am I doing wrong here?



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)

var clicksVerHayMas = 0;
function VerHayMas()
clicksVerHayMas = clicksVerHayMas +1;
if(clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
,4000);
;










share|improve this question



















  • 1





    You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

    – Alicia
    May 4 at 0:21











  • If you simply want to hide something on landscape orientation, why not use one line of CSS?

    – Salman A
    May 4 at 12:58











  • Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

    – Bergi
    May 4 at 13:07













7












7








7








I am running a script to show a notification within a menu with scroll, but I do not know how to detect if the device has orientation landscape to validate the script.



The call onClick="VerHayMas();" works perfectly, but if the user open the menu once, clicking on #boton-menu and with your device in portrait, after changing the orientation to landscape the script no longer meet the objective.




The script has its logic ONLY if the device is in landscape, which is
when the menu needs to show the notification.




So, is it possible that my script is only valid with (max-width:999px) and (orientation:landscape), ignoring the portrait...?



I am a beginner in JS, and I do not know how to do it, really.



Any idea?



Thanks in advance!



HTML & CSS



#mas-menu display:none

<div id="boton-menu" onClick="VerHayMas();">+</div>


Script:



var clicksVerHayMas = 0;

function VerHayMas()
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);

;


EDIT:



I have tried with the following script, but it does not work. If the user makes a call to onClick="VerHayMas();" in portrait mode, the script is no longer running in landscape mode.



What am I doing wrong here?



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)

var clicksVerHayMas = 0;
function VerHayMas()
clicksVerHayMas = clicksVerHayMas +1;
if(clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
,4000);
;










share|improve this question
















I am running a script to show a notification within a menu with scroll, but I do not know how to detect if the device has orientation landscape to validate the script.



The call onClick="VerHayMas();" works perfectly, but if the user open the menu once, clicking on #boton-menu and with your device in portrait, after changing the orientation to landscape the script no longer meet the objective.




The script has its logic ONLY if the device is in landscape, which is
when the menu needs to show the notification.




So, is it possible that my script is only valid with (max-width:999px) and (orientation:landscape), ignoring the portrait...?



I am a beginner in JS, and I do not know how to do it, really.



Any idea?



Thanks in advance!



HTML & CSS



#mas-menu display:none

<div id="boton-menu" onClick="VerHayMas();">+</div>


Script:



var clicksVerHayMas = 0;

function VerHayMas()
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);

;


EDIT:



I have tried with the following script, but it does not work. If the user makes a call to onClick="VerHayMas();" in portrait mode, the script is no longer running in landscape mode.



What am I doing wrong here?



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)

var clicksVerHayMas = 0;
function VerHayMas()
clicksVerHayMas = clicksVerHayMas +1;
if(clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
,4000);
;







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 4 at 0:59







Pablo_Web

















asked May 4 at 0:14









Pablo_WebPablo_Web

1268




1268







  • 1





    You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

    – Alicia
    May 4 at 0:21











  • If you simply want to hide something on landscape orientation, why not use one line of CSS?

    – Salman A
    May 4 at 12:58











  • Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

    – Bergi
    May 4 at 13:07












  • 1





    You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

    – Alicia
    May 4 at 0:21











  • If you simply want to hide something on landscape orientation, why not use one line of CSS?

    – Salman A
    May 4 at 12:58











  • Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

    – Bergi
    May 4 at 13:07







1




1





You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

– Alicia
May 4 at 0:21





You can get the width and height of a window with window.innerHeight and window.innerWidth respectively. How about keep it simple, if height is less than width, the user is in landscape mode.

– Alicia
May 4 at 0:21













If you simply want to hide something on landscape orientation, why not use one line of CSS?

– Salman A
May 4 at 12:58





If you simply want to hide something on landscape orientation, why not use one line of CSS?

– Salman A
May 4 at 12:58













Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

– Bergi
May 4 at 13:07





Your attempt at putting the function declaration inside the if statement does not work because then it is no longer a global that can be called from the inline event handler attribute.

– Bergi
May 4 at 13:07












2 Answers
2






active

oldest

votes


















6














You can solve this using matchMedia:



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)
// do something



Make sure to note the browser support in the MDN link.



EDIT TO PROVIDE CONTEXT:



Because the user may be moving around their screen, you will want to make this evaluation inside VerHayMas, each time it is run, to determine if the main body of the script should be executed:



var clicksVerHayMas = 0;

function VerHayMas()
var isLandscapeAndMeetsSizeRequirements = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (isLandscapeAndMeetsSizeRequirements)
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);


;


So VerHayMas will be run on every click, but only if the screen meets the requirements as determined by the media query string will it execute the code inside the if block.






share|improve this answer




















  • 1





    Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

    – Alexander Nied
    May 4 at 1:07












  • @Pablo_Web - Updated answer with an example showing how this would work.

    – Alexander Nied
    May 4 at 1:21











  • Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

    – Pablo_Web
    May 4 at 1:27


















9














I'd keep it simple, if screen height is less than width, then the user is in landscape mode. You can grab the height and width from the global window object.



if (window.innerWidth > window.innerHeight) 
// The user is in landscape mode!
userInLanscapeFunc();



Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight






share|improve this answer


















  • 2





    Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

    – ᆼᆺᆼ
    May 4 at 0:29











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55978407%2fjavascript-run-my-script-only-if-landscape-is-detected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














You can solve this using matchMedia:



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)
// do something



Make sure to note the browser support in the MDN link.



EDIT TO PROVIDE CONTEXT:



Because the user may be moving around their screen, you will want to make this evaluation inside VerHayMas, each time it is run, to determine if the main body of the script should be executed:



var clicksVerHayMas = 0;

function VerHayMas()
var isLandscapeAndMeetsSizeRequirements = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (isLandscapeAndMeetsSizeRequirements)
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);


;


So VerHayMas will be run on every click, but only if the screen meets the requirements as determined by the media query string will it execute the code inside the if block.






share|improve this answer




















  • 1





    Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

    – Alexander Nied
    May 4 at 1:07












  • @Pablo_Web - Updated answer with an example showing how this would work.

    – Alexander Nied
    May 4 at 1:21











  • Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

    – Pablo_Web
    May 4 at 1:27















6














You can solve this using matchMedia:



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)
// do something



Make sure to note the browser support in the MDN link.



EDIT TO PROVIDE CONTEXT:



Because the user may be moving around their screen, you will want to make this evaluation inside VerHayMas, each time it is run, to determine if the main body of the script should be executed:



var clicksVerHayMas = 0;

function VerHayMas()
var isLandscapeAndMeetsSizeRequirements = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (isLandscapeAndMeetsSizeRequirements)
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);


;


So VerHayMas will be run on every click, but only if the screen meets the requirements as determined by the media query string will it execute the code inside the if block.






share|improve this answer




















  • 1





    Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

    – Alexander Nied
    May 4 at 1:07












  • @Pablo_Web - Updated answer with an example showing how this would work.

    – Alexander Nied
    May 4 at 1:21











  • Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

    – Pablo_Web
    May 4 at 1:27













6












6








6







You can solve this using matchMedia:



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)
// do something



Make sure to note the browser support in the MDN link.



EDIT TO PROVIDE CONTEXT:



Because the user may be moving around their screen, you will want to make this evaluation inside VerHayMas, each time it is run, to determine if the main body of the script should be executed:



var clicksVerHayMas = 0;

function VerHayMas()
var isLandscapeAndMeetsSizeRequirements = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (isLandscapeAndMeetsSizeRequirements)
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);


;


So VerHayMas will be run on every click, but only if the screen meets the requirements as determined by the media query string will it execute the code inside the if block.






share|improve this answer















You can solve this using matchMedia:



const matchesMediaQuery = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (matchesMediaQuery)
// do something



Make sure to note the browser support in the MDN link.



EDIT TO PROVIDE CONTEXT:



Because the user may be moving around their screen, you will want to make this evaluation inside VerHayMas, each time it is run, to determine if the main body of the script should be executed:



var clicksVerHayMas = 0;

function VerHayMas()
var isLandscapeAndMeetsSizeRequirements = window.matchMedia('(max-width:999px) and (orientation:landscape)').matches;

if (isLandscapeAndMeetsSizeRequirements)
clicksVerHayMas = clicksVerHayMas + 1;
if (clicksVerHayMas == 1)
document.getElementById('mas-menu').style.display = 'block';

window.setTimeout(function()
$('#mas-menu').fadeOut('slow');
, 4000);


;


So VerHayMas will be run on every click, but only if the screen meets the requirements as determined by the media query string will it execute the code inside the if block.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 4 at 1:21

























answered May 4 at 0:23









Alexander NiedAlexander Nied

4,41211130




4,41211130







  • 1





    Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

    – Alexander Nied
    May 4 at 1:07












  • @Pablo_Web - Updated answer with an example showing how this would work.

    – Alexander Nied
    May 4 at 1:21











  • Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

    – Pablo_Web
    May 4 at 1:27












  • 1





    Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

    – Alexander Nied
    May 4 at 1:07












  • @Pablo_Web - Updated answer with an example showing how this would work.

    – Alexander Nied
    May 4 at 1:21











  • Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

    – Pablo_Web
    May 4 at 1:27







1




1





Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

– Alexander Nied
May 4 at 1:07






Couldn't you simply run that matchMedia inside of VeryHayMas so that it evaluates the screen size and orientation immediately before it needs to act on that information? Of course there orientation may change, so you will need to run it each time you make the check.

– Alexander Nied
May 4 at 1:07














@Pablo_Web - Updated answer with an example showing how this would work.

– Alexander Nied
May 4 at 1:21





@Pablo_Web - Updated answer with an example showing how this would work.

– Alexander Nied
May 4 at 1:21













Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

– Pablo_Web
May 4 at 1:27





Alexander Nied ... Now I understand. I was doing it wrong. I will study your script. It is very clean and tidy. Thank you!. Greetings...

– Pablo_Web
May 4 at 1:27













9














I'd keep it simple, if screen height is less than width, then the user is in landscape mode. You can grab the height and width from the global window object.



if (window.innerWidth > window.innerHeight) 
// The user is in landscape mode!
userInLanscapeFunc();



Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight






share|improve this answer


















  • 2





    Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

    – ᆼᆺᆼ
    May 4 at 0:29















9














I'd keep it simple, if screen height is less than width, then the user is in landscape mode. You can grab the height and width from the global window object.



if (window.innerWidth > window.innerHeight) 
// The user is in landscape mode!
userInLanscapeFunc();



Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight






share|improve this answer


















  • 2





    Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

    – ᆼᆺᆼ
    May 4 at 0:29













9












9








9







I'd keep it simple, if screen height is less than width, then the user is in landscape mode. You can grab the height and width from the global window object.



if (window.innerWidth > window.innerHeight) 
// The user is in landscape mode!
userInLanscapeFunc();



Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight






share|improve this answer













I'd keep it simple, if screen height is less than width, then the user is in landscape mode. You can grab the height and width from the global window object.



if (window.innerWidth > window.innerHeight) 
// The user is in landscape mode!
userInLanscapeFunc();



Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight







share|improve this answer












share|improve this answer



share|improve this answer










answered May 4 at 0:24









AliciaAlicia

1,87422138




1,87422138







  • 2





    Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

    – ᆼᆺᆼ
    May 4 at 0:29












  • 2





    Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

    – ᆼᆺᆼ
    May 4 at 0:29







2




2





Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

– ᆼᆺᆼ
May 4 at 0:29





Maybe it would be better to use screen.width/.height though it depends if they actually care about the shape of the available window area or the screen orientation

– ᆼᆺᆼ
May 4 at 0:29

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55978407%2fjavascript-run-my-script-only-if-landscape-is-detected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?