[英]Detect if window has scrollbar in IE using javascript (window.open)
When i call window.open, I can include a list of parameters. 当我调用window.open时,我可以包含一个参数列表。 One of these paramaters is scrollbars, which can be set to yes or no .
其中一个参数是滚动条,可以设置为是或否 。
When I put javascript in the child window, the javascript needs to detect if scrollbars were set to yes or no when the window was opened. 当我在子窗口中放置javascript时,javascript需要检测窗口打开时是否将滚动条设置为yes或no。 I want to know if the window has scrollbars enabled by default or not.
我想知道窗口是否默认启用了滚动条。
I only care about doing this in IE. 我只关心在IE中这样做。 How do I check?
我该如何检查? window.scroolbar does not work in IE.
window.scroolbar在IE中不起作用。
How do I do this? 我该怎么做呢? To be perfectly clear, I'm not talking about div overflows, I'm talking about the scrollbar property of the window.
要非常清楚,我不是在谈论div溢出,我在谈论窗口的滚动条属性。
edit: 编辑:
- I am in IE so window.scrollbars/this.scrollbars won't return anything - 我在IE中,因此window.scrollbars / this.scrollbars不会返回任何内容
- The windows scrollbars exist outside the body. - 窗户滚动条存在于身体外部。
- Looking at the document's width will tell me about the document. - 查看文档的宽度将告诉我有关该文档的信息。 I can even figure out if there are scrollbars in the document.
我甚至可以弄清楚文档中是否有滚动条。 This will not tell me anything about the window itself.
这不会告诉我有关窗口本身的任何信息。
- The width of the window's scrollbar changes dependent on what the currently selected Windows Desktop Theme is, for ascetic reasons. - 由于苦行僧的原因,窗口滚动条的宽度会根据当前所选的Windows桌面主题的变化而变化。
Alongside your script that opens the child window (the one where you set scrollbars=yes or no), add a window-level variable that's true if scrollbars=yes, or false if no. 除了打开子窗口(设置滚动条= yes或no)的脚本旁边,添加一个窗口级变量,如果scrollbars = yes则为true,否则为false。
Then in your child window's script, you look up the value that's been set from self.opener.myWindowLevelVariable
. 然后在子窗口的脚本中,查找从
self.opener.myWindowLevelVariable
设置的值。
You could also namespace the variable. 您也可以命名变量。 The important part is self.opener or window.opener if you prefer.
如果您愿意,重要的部分是self.opener或window.opener。
Update: 更新:
In response to your update about not wanting to use a variable in the parent...Then reverse my initial suggestion. 响应您的更新,不想在父级中使用变量...然后反转我的初始建议。 Put the variable in the child when it's created.
将变量放在子项创建时。
Parent: 家长:
var scrollwindow = window.open("file.htm", "anotherwindow", "width=400,height=250,scrollbars=no");
scrollwindow.hasScrollbars = false;
Child: 儿童:
alert(hasScrollbars);
If you want to handle the case where the child window is opened directly, then it gets more interesting... 如果你想处理直接打开子窗口的情况,那么它会变得更有趣......
Child: 儿童:
try {
// do something with hasScrollbars
// If it isn't true or false, ie undefined, using it will throw you into the catch.
alert(hasScrollbar);
} catch (e) {
// scrollbars weren't explicitly added or forbidden, so they'll automatically
// show up if the content is larger than the window. In this case, use a
// scrollbar sniffing technique.
var hasVerticalScrollbar = document.body.scrollHeight > document.body.clientHeight;
var hasHorizontalScrollbar = document.body.scrollWidth > document.body.clientWidth;
}
Scrollbar sniffing: I think this is what Stephano was going for. 滚动条嗅探:我认为这就是Stephano的目标。 He was on the right track.
他走在正确的轨道上。 But use clientWidth, scrollWidth, clientHeight, and scrollHeight in combination.
但是组合使用clientWidth,scrollWidth,clientHeight和scrollHeight。 From quirks mode :
从怪癖模式 :
If the element has no scrollbars scrollWidth/Height should be equal to clientWidth/Height.
如果元素没有滚动条,则scrollWidth / Height应该等于clientWidth / Height。
When the element has no scrollbars IE makes the scrollHeight equal to the actual height of the content;
当元素没有滚动条时,IE使scrollHeight等于内容的实际高度; and not the height of the element.
而不是元素的高度。 scrollWidth is correct, except in IE8, where it's 5 pixels off.
scrollWidth是正确的,除了在IE8中,它关闭了5个像素。
So, you'll have to adjust the scrollbar sniffing part a bit for IE, but that's the basic idea. 所以,你必须为IE调整滚动条嗅探部分,但这是基本的想法。
You can determine if the window has a visible scrollbar in IE by using this little JavaScript trick: 您可以通过使用这个小技巧来确定窗口是否在IE中有可见的滚动条:
//You'll have to modify this so as not to do it unless your user is running IE
window.attachEvent('onload', getChrome);
function getChrome() {
//read the window width and height
var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;
//set the window to that size
window.resizeTo(w, h);
//read the window width and height again
var newW = document.documentElement.clientWidth;
var newH = document.documentElement.clientHeight;
//calculate the difference
var diffX = w - newW;
var diffY = h - newH;
//set the window back to what it was
window.resizeBy(diffX, diffY);
alert('diffX: ' + diffX + '\ndiffY: ' + diffY);
//If diffX is larger than 10 (in Vista and Windows 7, the borders are 5px each)
//then you're scrollbar is visible.
}
This is a little hacky, but it seems to work: 这有点hacky,但它似乎工作:
function has_scrollbar() {
if (document.documentElement.clientHeight < document.body.offsetHeight) {
alert("vertical scrollbar");
} else {
alert("no vertical scrollbar");
}
}
You check the size of the offsetHeight (html content) and compare it with the documentElement.clientHeight (window height for IE). 检查offsetHeight(html内容)的大小,并将其与documentElement.clientHeight(IE的窗口高度)进行比较。 You can switch out "width" for "height", obviously.
显然,您可以为“高度”切换“宽度”。
Hope this helps! 希望这可以帮助!
Try this 尝试这个
scrollwindow = window.open("file.htm", "anotherwindow", "width=400,height=250");
if (scrollwindow.scrollbars)
{
alert("Yes we have scrollbars");
}
else
{
alert("Sorry doesnt support scrollbars");
}
You can check document height , then check window height and if document height is a bigger number, then you have scrollbars. 您可以检查文档高度 ,然后检查窗口高度 ,如果文档高度是一个更大的数字,那么您有滚动条。
However, because IE will display always scrollbars (even if you don't have anything to scroll) you may want to set overvlow:auto
for body tag. 但是,因为IE将始终显示滚动条(即使您没有要滚动的内容),您可能需要设置
overvlow:auto
for body tag。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.