0

I have a function to calculate the screen width

var screenWidth = jQuery(window).width();

function checkScreenSize()
{   
    screenWidth = jQuery(window).width();
}

jQuery(window).on("resize", checkScreenSize).resize();  

How can I use this screenWidth inside another function?

EDIT:

I want to access this variable here, for example:

function showContent() {
    if(screenWidth <= 1000)
    {
         console.log(screenWidth);
    }
}
2
  • 1
    where did you try to access it and its not available? Commented Jan 8, 2017 at 11:11
  • I think what @Deep wanted to know is, where do you define this function at where do you call it. If it is called before the checkScreenSize Method has completed then, the variable has not been set with the width value. Do you call it every time a resize event happens? How do you synchronize the two methods? If schowContent is defined outside of the scope where the var screenWith variable (which is not nessecarily a "global" variable; you could have defined it in another function body or in a requirejs module) is defined then, the screenWith will be unknown to the showContent Method. Commented Jan 8, 2017 at 11:44

4 Answers 4

1

Since screenWidth is a global variable declared outside the function you have mentioned, you can access it as it is:

function foo() {
    console.log(screenWidth);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I should have said that I need the screen width to update when the window is size also
@KeithDonegan your global variable is accessible from anywhere. If you need to update it on different actions, then you need to add handlers for that purpose. You already have a resize handler. How is this not working and what do you want to achieve instead?
0

Your global screenWidthis quite useless...

if you need to know the width inside the resize handler callback that's where you need to call jQuery(window).width()

function checkScreenSize(){
  var w = $(window).width()
  console.log(w)
}

jQuery(window).on("resize", checkScreenSize)

// .resize() is a shorthand for .on('resize' method
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0


when you defined variable out of function , it will be global variable and you can use it in other function !!
do you have problme with this ?


you can also visit : http://www.w3schools.com/js/js_scope.asp

Comments

0

You can achieve this by defining said variable in global scope; if that var screenWidth = ... is defined inside some other function context you will not have access to it from other functions.

Using globals is a bad idea; however, in many cases you need some variable value that you need access to from other places. In this case, you could construct a well defined "Globals" container, like i.e:

'use strict';        // strict mode    :: prevents nasty bugs
this.Main = this;    // super-global   :: refers `window` -or- `global`

Main.Globals = {screenWidth:null};

// your dynamic screen resizing code here sets the `Globals.screenWidth`
// now you have access to this anywhere

You can also write some code to have this Globals object be only writable by certain functions, but this technique is probably out of the scope of this question; however it could be a good idea considering that this code is part of a large project with many collaborators in your team.

1 Comment

If you need a more elaborate answer that includes dynamically setting the values upon window resize event; or even if I should add the write restrictions code - just pop a comment here and I'll add it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.