admin管理员组

文章数量:1333404

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 pensate 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 :)

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 pensate 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 :)

Share Improve this question asked Apr 24, 2011 at 7:56 clarkkclarkk 1 3
  • What do you mean by "body width"? Please add more detail – Pekka Commented Apr 24, 2011 at 7:58
  • Re your edit: I don't think changing the body's width will help you in that case. Is the dialog box an iframe? – Pekka Commented Apr 24, 2011 at 8:00
  • no.. its a div box placed in the center and middle – clarkk Commented Apr 24, 2011 at 8:01
Add a ment  | 

2 Answers 2

Reset to default 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';
  }
};

If the problem is the scroll bar ing 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.

本文标签: javascriptset body width in pxStack Overflow