Flip
Flip

Reputation: 6761

Javascript: Code doesn't get executed when put into function

I have this working Javascript code which does some resizing on init and resize:

(function (w) {

  w.init = function () {

    var w = window.innerWidth;
    var h = window.innerHeight; 

    //resize_font;
    var fs = parseInt(w / 26);   
    document.body.style.fontSize = fs + "px";

    //resize_cover;
    var logo = get_by_id("logo");
    var logo_height = logo.clientHeight;  
    var menu = get_by_id("menu");
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height;
    var distance_top = (h - cover_height) / 2.5;

    var container = get_by_id("container");
    container.style.paddingTop = distance_top + "px"; 
  }

  w.resize = function () {

    var w = window.innerWidth;
    var h = window.innerHeight; 

    //resize_font;
    var fs = parseInt(w / 26);   
    document.body.style.fontSize = fs + "px";

    //resize_cover;
    var logo = get_by_id("logo");
    var logo_height = logo.clientHeight;  
    var menu = get_by_id("menu");
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height;
    var distance_top = (h - cover_height) / 2.5;

    var container = get_by_id("container");
    container.style.paddingTop = distance_top + "px"; 
  }
}

Now I want to refactor the code for resizing font and cover into a function, but I can't get it working. I tried this:

(function (w) {

  w.init = function () {
    var w = window.innerWidth;
    var h = window.innerHeight; 

    resize_font;
    resize_cover;
  }

  w.resize = function () {
    var w = window.innerWidth;
    var h = window.innerHeight; 

    resize_font;
    resize_cover;
  }

  function resize_cover() {
    var logo = get_by_id("logo");
    var logo_height = logo.clientHeight;  
    var menu = get_by_id("menu");
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height;
    var distance_top = (h - cover_height) / 2.5;

    var container = get_by_id("container");
    container.style.paddingTop = distance_top + "px";        
  }

  function resize_font() {
    var fs = parseInt(w / 26);   
    document.body.style.fontSize = fs + "px";        
  }
}

The console doesn'T show any errors, but resizing isn't happening. What am I doing wrong?

Upvotes: 0

Views: 52

Answers (3)

kutomer
kutomer

Reputation: 734

It's seems like you forgot to execute the functions, just mentioned the pointer.

please try to change:

resize_font;
resize_cover;

to

resize_font();
resize_cover();

Upvotes: 1

TobotRobot
TobotRobot

Reputation: 54

Looks like your problem is this:

resize_font;
resize_cover;

I think you mean

resize_font();
resize_cover();

Upvotes: 2

SethWhite
SethWhite

Reputation: 1977

You need to invoke your functions in your init and resize event functions. You have:

resize_font;
resize_cover;

You need:

resize_font();
resize_cover();

The name of a function is just a reference to it, which is why you don't see any errors. What you currently have is similar to saying:

var x = 1;
x;

Upvotes: 3

Related Questions