1

My website needs to convert its nav menu when it is in a viewport which size is less than 800px.More over I have made a plugin for this and that plugin also provide vertical nav options.So i want to apply that vertical nav when it is in viewport less than 800px.

My codes for vertical nav

if(verticalNav){
   menuConvert()
}

And when in less than 800px

if($(window).width < 800){
   menuConvert()
}

And menuConvert function is

var menuConvert = function(){
     // codes here
}

But in console log: menuConvert is not a function

Any solution for that?

3
  • CSS media queries will be better for this @media screen and(max-width:800px){ //put your style in here; } Commented May 26, 2015 at 5:22
  • 2
    I think the problem is var menuConvert = function(){ is executed after menuConvert() call Commented May 26, 2015 at 5:22
  • Are you loading your javascript file at the bottom of your <body></body> tags? Commented May 26, 2015 at 5:23

2 Answers 2

3
var menuConvert = function(){
     // codes here
}

should be PLACED above

if($(window).width < 800){
 menuConvert();
}

OR change assignment to simple function like this:

function menuConvert(){
   console.log("Hello")
}

and it should work. The reason is JavaScript only hoists declarations, not initializations.

Sign up to request clarification or add additional context in comments.

Comments

0

In Javascript there is a difference between

var a = function() { ... };

and

function a() { ... }

The first is an assignment and is executed where the statement is met during the normal execution flow. This means for example that you cannot call a() before it has been set to be a function/closure.

The second is instead a function declaration and for example code like

console.log(b());
function b() { return 42; }

is valid.

Comments

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.