使用 javascript (window.open) 检测 IE 中窗口是否有滚动条
当我调用 window.open 时,我可以包含参数列表。这些参数之一是滚动条,它可以设置为yes或no。
当我将 JavaScript 放入子窗口中时,JavaScript 需要检测打开窗口时滚动条是否设置为“是”或“否”。我想知道窗口是否默认启用滚动条。
我只关心在 IE 中执行此操作。我该如何检查? window.scroolbar 在 IE 中不起作用。
我该怎么做?明确地说,我不是在谈论 div 溢出,而是在谈论窗口的滚动条属性。
编辑:
- 我在 IE 中,所以 window.scrollbars/this.scrollbars 不会返回任何内容
- 窗口滚动条存在于主体之外。
- 查看文档的宽度将告诉我有关该文档的信息。我什至可以弄清楚文档中是否有滚动条。这不会告诉我有关窗口本身的任何信息。
- 出于禁欲的原因,窗口滚动条的宽度根据当前选择的 Windows 桌面主题而变化。
When i call window.open, I can include a list of parameters. 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. I want to know if the window has scrollbars enabled by default or not.
I only care about doing this in IE. How do I check? window.scroolbar does not work in 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.
edit:
- I am in IE so window.scrollbars/this.scrollbars won't return anything
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了打开子窗口(设置scrollbars=yes 或no 的脚本)的脚本之外,添加一个窗口级变量,如果scrollbars=yes 则为true,否则为false。
然后,在子窗口的脚本中,查找从
self.opener.myWindowLevelVariable
设置的值。您还可以为变量命名空间。如果您愿意,重要的部分是 self.opener 或 window.opener。
更新:
响应您关于不想在父级中使用变量的更新...然后推翻我最初的建议。创建子级时将变量放入其中。
Parent:
Child:
如果你想处理直接打开子窗口的情况,那么它会变得更有趣...
Child:
滚动条嗅探: 我认为这就是 Stephano 的目的。他走在正确的轨道上。但是结合使用 clientWidth、scrollWidth、clientHeight 和scrollHeight。来自怪癖模式:
因此,您必须为 IE 稍微调整滚动条嗅探部分,但这就是基本思想。
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.
Then in your child window's script, you look up the value that's been set from
self.opener.myWindowLevelVariable
.You could also namespace the variable. The important part is self.opener or window.opener if you prefer.
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:
Child:
If you want to handle the case where the child window is opened directly, then it gets more interesting...
Child:
Scrollbar sniffing: I think this is what Stephano was going for. He was on the right track. But use clientWidth, scrollWidth, clientHeight, and scrollHeight in combination. From quirks mode:
So, you'll have to adjust the scrollbar sniffing part a bit for IE, but that's the basic idea.
您可以使用以下 JavaScript 小技巧来确定窗口在 IE 中是否有可见的滚动条:
You can determine if the window has a visible scrollbar in IE by using this little JavaScript trick:
这有点 hacky,但似乎有效:
您检查 offsetHeight(html 内容)的大小并将其与 documentElement.clientHeight(IE 的窗口高度)进行比较。显然,您可以将“宽度”切换为“高度”。
希望这有帮助!
This is a little hacky, but it seems to work:
You check the size of the offsetHeight (html content) and compare it with the documentElement.clientHeight (window height for IE). You can switch out "width" for "height", obviously.
Hope this helps!
您可以检查文档高度,然后检查窗口高度 如果文档高度是一个更大的数字,那么你就有滚动条。
但是,由于 IE 将始终显示滚动条(即使您没有任何可滚动的内容),因此您可能需要为 body 标记设置
overvlow:auto
。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.试试这个
Try this