8

In LESS I used following code to get the window's height.

@winheight:`$(window).height()`

What I'm getting is a number, but when i add px there to have the unit,

height: @winheight px;

It will compile to something like height: 910 px.

I tried to have the unit after the javascript evaluation too. but I got the same result.

@winheight:`$(window).height()`px
height: @winheight;
...

height:910 px;

How can I get height:910px there (without the space between number and unit) ?


EDIT:

As for the first four results, it creates a string height:"910px", which doesn't render correctly.

Hidde
  • 11,493
  • 8
  • 43
  • 68
tiran
  • 2,389
  • 1
  • 16
  • 28

7 Answers7

7

Simply use string interpolation and then escape from the string using ~:

@winheight:`$(window).height()`;

height: ~"@{winheight}px";
Kyle Cureau
  • 19,028
  • 23
  • 75
  • 104
Paul
  • 139,544
  • 27
  • 275
  • 264
1

give this code and see what is you get it.

@winheight:0px + `$(window).height()'
gnat
  • 6,213
  • 108
  • 53
  • 73
R' Zone
  • 67
  • 5
1

Take .css(height) instead of .height() - this returns the value + unit.

Christoph
  • 50,121
  • 21
  • 99
  • 128
0

Does replacing empty space help for you? Try reading the answer of this: Replacing spaces with underscores in JavaScript?

Community
  • 1
  • 1
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
0

try this

@winheight:`$(window).height()+"px"`
height: @winheight;

because .height() returns only unit-less pixel value.
alternatively use the following

@winheight:`$(window).css("height")`
    height: @winheight;

.css("height") returns a value with units

Valli69
  • 8,856
  • 4
  • 23
  • 29
  • this returns a string value. but it is not rendered in the browser `height: "910px"` – tiran May 29 '12 at 06:58
  • @tiran check my updated answer once, please let me know if is working or not – Valli69 May 29 '12 at 07:06
  • I got following error when I execute your code in the browser. `TypeError: Cannot read property 'defaultView' of undefined` – tiran May 29 '12 at 07:11
0

What about this:

@winheight:`$(window).height().toString() + "px"`
Matthew
  • 12,892
  • 6
  • 42
  • 45
0

This might work depending on LESS which I do not know well.

Reading the docs this is a possibility.

@winheight:0px + `$(window).height()`
HBP
  • 15,685
  • 6
  • 28
  • 34