2

how can you set the body width in px in javascript?

has to work in chrome, ff and our beloved ie

edit:

it has to be used when a dialog box pops up and the horisontal scrollbar is hidden.. then I have to compensate for the missing 16px.. else the whole site is moving slightly to the right

maybe you have better solution to the problem? I don't know :)

clarkk
  • 25,706
  • 67
  • 182
  • 315

2 Answers2

4
document.body.style.width = '800px';

[Edit]

If you need to adjust from the existing width, per your edit, you could do something like this:

var adjustBodyWidth = function(nPixels) {
  var m, w = document.body.style.width || document.body.clientWidth;
  if (m=w.match(/^(\d+)px$/)) {
    document.body.style.width = (Number(m[1]) + nPixels) + 'px';
  }
};
maerics
  • 143,080
  • 41
  • 260
  • 285
0

If the problem is the scroll bar coming up and disappearing depending on the page's contents, I would consider forcing the page to display a (disabled) scroll bar even when there's nothing to scroll. This can be done through CSS:

body { overflow-y: scroll }

it works in all major browsers. It doesn't in IE, but that doesn't matter because IE shows the desired behaviour automatically.

Pekka
  • 431,103
  • 135
  • 960
  • 1,075