IE8——focus函数不好用
Option Explicit Dim win Sub Activate win.focus win.document.focus If win.document.hasFocus() Then Exit Sub Dim wShell Set wShell=CreateObject("WScript.Shell") If wShell.AppActivate(win.document.title) Then Exit Sub Dim LocationURL LocationURL=win.location.href If Left(LocationURL,8)="file:///" Then LocationURL=Replace(UnEscape(Mid(LocationURL,9)),"/","/") If LocationURL="about:blank" Then LocationURL="空白页" If wShell.AppActivate(LocationURL) Then Exit Sub End Sub
4 如果Activex控件也不想利用的话,可以用下面的方法。
Sub MsgActivate win.focus win.document.focus If win.document.hasFocus() Then Exit Sub win.setTimeout "MsgBox ""focus moving..."",vbSystemModal",0,"vbscript" End Sub
下面是一个完整的例子。
<html> <head> <title>opener</title> <object id=wShell classid=clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8></object> <script language=vbscript> Option Explicit Dim win Sub Activate(win) win.focus win.document.focus If win.document.hasFocus() Then Exit Sub Dim wShell Set wShell=CreateObject("WScript.Shell") If wShell.AppActivate(win.document.title) Then Exit Sub Dim LocationURL LocationURL=win.location.href If Left(LocationURL,8)="file:///" Then LocationURL=Replace(UnEscape(Mid(LocationURL,9)),"/","/") If LocationURL="about:blank" Then LocationURL="空白のページ" If wShell.AppActivate(LocationURL) Then Exit Sub End Sub Sub MsgActivate(win) win.focus win.document.focus If win.document.hasFocus() Then Exit Sub win.setTimeout "MsgBox ""focus moving..."",vbSystemModal",0,"vbscript" End Sub </script> <script language="javascript" type="text/javascript"> <!-- var newWin = null; var myurl = 'child.htm'; function OpenNewWindow() { newWin = window.open('child.htm','popup','width=550, height=262, toolbar=0, resizable=false'); } function FocusNewWindowByActivate(){ if (newWin != null && !newWin.closed){ Activate(newWin); } } function FocusNewWindowByFocus(){ if (newWin != null && !newWin.closed){ newWin.focus(); } } function FocusNewWindowByMsgActivate(){ if (newWin != null && !newWin.closed){ MsgActivate(newWin); } } --> </script> </head> <body> <button onclick='OpenNewWindow()'>open child.htm in window</button> <button onclick="FocusNewWindowByFocus()">FocusNewWindowByFocus</button> <button onclick="FocusNewWindowByActivate()">FocusNewWindowByActivate</button> <button onclick="FocusNewWindowByMsgActivate()">FocusNewWindowByMsgActivate</button> </body> </html>
- 已标记为答案hatzhang 2009年8月20日 7:26
- 回复
- 引用
全部回复
- 2009年8月20日 7:26hatzhang0
登录进行投票
这个问题在日本的msdb论坛上(下面的URL)也有讨论,并且有了一定的结论,现在把结果转过来。http://social.msdn.microsoft.com/Forums/ja-JP/internetexplorerja/thread/3a7e083c-c75c-4cac-ad23-a733ede6e04c首先是这一现象的原因的简单分析。从下面的url来看,微软宣称,从ie8开始,focus函数不再把激活的窗口带到最前面,而仅仅使之在任务栏闪烁。http://msdn.microsoft.com/ja-jp/library/ms536425(en-us,VS.85).aspx可从实际的测试中来看,在有些环境下,focus仍然有时候会成功(成功:会把窗口带到最前面,失败:仅仅在任务栏闪烁)。进一步的调查发现,在系统参数FOREGROUNDLOCKTIMEOUT设置为0时,focus的表现为有时成功,有时失败,而当FOREGROUNDLOCKTIMEOUT设置为200000时(系统的初始值),focus的表现为 完全失败。根据推测,focus也应该是使用了SetForegroundWindow的方法来将窗口带到最前面(Z-order的顶端,前景窗口)。由于ie8使用了LCIE的进程分离技术,父窗口和子窗口不再处于一个进程。当父窗口处于最前方的时候,子窗口所属进程就不再是foreground。从而导致使SetForegroundWindow成功需要的前提条件,不再符合。所以这一现象,更有可能是由于LCIE导致的边缘效果。FOREGROUNDLOCKTIMEOUT:http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspxSetForegroundWindow:http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx这一现象有以下的回避或者解决手段。1 LCIE无效(TabProcGrowth=1)这样,父窗口和子窗口处于一个进程,focus就总能成功。不过,一是要修改注册表,二是不能再享受LCIE带来的好处。2 FOREGROUNDLOCKTIMEOUT设为0,连续调用focus(比如说,用一个100回的循环)使用javascript和activeX修改FOREGROUNDLOCKTIMEOUT的方法如下(需要有安装Excel):将下面的命令敲入ie的地址栏,回车即可。TIMEOUT=0(focus有时成功,有时失败)javascript:alert( (new ActiveXObject("Excel.Application")).ExecuteExcel4Macro('CALL("user32","SystemParametersInfoA","JJJJJ",8193,0,0,0)')?"设定完了!":"" );TIMEOUT=200000(系统的初始值)javascript:alert( (new ActiveXObject("Excel.Application")).ExecuteExcel4Macro('CALL("user32","SystemParametersInfoA","JJJJJ",8193,0,200000,0)')?"设定完了!":"" );事实上,由于很多软件在安装的时候都会改变FOREGROUNDLOCKTIMEOUT。参照上面的SetForegroundWindow的url。3 如果不想改变系统设定的话,可以使用WScript.Shell这个Activex控件。使用WScript.Shell的AppActivate方法。Option Explicit Dim win Sub Activate win.focus win.document.focus If win.document.hasFocus() Then Exit Sub Dim wShell Set wShell=CreateObject("WScript.Shell") If wShell.AppActivate(win.document.title) Then Exit Sub Dim LocationURL LocationURL=win.location.href If Left(LocationURL,8)="file:///" Then LocationURL=Replace(UnEscape(Mid(LocationURL,9)),"/","/") If LocationURL="about:blank" Then LocationURL="空白页" If wShell.AppActivate(LocationURL) Then Exit Sub End Sub
更复杂的版本,请参照下面的url。http://scripting.cocolog-nifty.com/blog/2009/08/ie7ie8windowope.html4 如果Activex控件也不想利用的话,可以用下面的方法。
不过这个方法,会弹出【focus moving...】的警告窗口。Sub MsgActivate win.focus win.document.focus If win.document.hasFocus() Then Exit Sub win.setTimeout "MsgBox ""focus moving..."",vbSystemModal",0,"vbscript" End Sub
下面是一个完整的例子。
<html> <head> <title>opener</title> <object id=wShell classid=clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8></object> <script language=vbscript> Option Explicit Dim win Sub Activate(win) win.focus win.document.focus If win.document.hasFocus() Then Exit Sub Dim wShell Set wShell=CreateObject("WScript.Shell") If wShell.AppActivate(win.document.title) Then Exit Sub Dim LocationURL LocationURL=win.location.href If Left(LocationURL,8)="file:///" Then LocationURL=Replace(UnEscape(Mid(LocationURL,9)),"/","/") If LocationURL="about:blank" Then LocationURL="空白のページ" If wShell.AppActivate(LocationURL) Then Exit Sub End Sub Sub MsgActivate(win) win.focus win.document.focus If win.document.hasFocus() Then Exit Sub win.setTimeout "MsgBox ""focus moving..."",vbSystemModal",0,"vbscript" End Sub </script> <script language="javascript" type="text/javascript"> <!-- var newWin = null; var myurl = 'child.htm'; function OpenNewWindow() { newWin = window.open('child.htm','popup','width=550, height=262, toolbar=0, resizable=false'); } function FocusNewWindowByActivate(){ if (newWin != null && !newWin.closed){ Activate(newWin); } } function FocusNewWindowByFocus(){ if (newWin != null && !newWin.closed){ newWin.focus(); } } function FocusNewWindowByMsgActivate(){ if (newWin != null && !newWin.closed){ MsgActivate(newWin); } } --> </script> </head> <body> <button onclick='OpenNewWindow()'>open child.htm in window</button> <button onclick="FocusNewWindowByFocus()">FocusNewWindowByFocus</button> <button onclick="FocusNewWindowByActivate()">FocusNewWindowByActivate</button> <button onclick="FocusNewWindowByMsgActivate()">FocusNewWindowByMsgActivate</button> </body> </html>
最后,有关这个现象还有一些没有解决的问题。除了上面提到的之外,focus的表现似乎还和其他条件有关。不过暂时就先到这儿了。也许以后的ie会修正这个问题。希望能对遇到类似问题的朋友有所帮助,祝大家愉快。
IE8——focus函数不好用相关推荐
- 获取焦点(focus函数不起作用)
我想在进入登录页面时自动获取用户名的焦点,用户名id为"j_username",可用下面的写法都不行: $("#j_username").focus(); 或者 ...
- vue iframe 中写script_vue: 单文件组件 render函数
使用vue-cli创建的vue项目,如何在这种项目中使用组建? 首先创建项目.启动项目 我们再来了解一下目录结构,src文件夹是写逻辑代码的地方,public是最终渲染到浏览器的地方. 在public ...
- render注册一个链接组件_vue: 单文件组件 render函数
使用vue-cli创建的vue项目,如何在这种项目中使用组建? 首先创建项目.启动项目 我们再来了解一下目录结构,src文件夹是写逻辑代码的地方,public是最终渲染到浏览器的地方. 在public ...
- JQuery学习之一:(加载函数,获取画面元素,一些简单的函数【each】【find】【focuse】【blur】)
前言: 最近项目一直很忙,没有时间对工作进行总结: 今天是2013年1月18日 项目UT结束,对项目中的JQUERY知识进行一下总结 1·如何加载JQuery函数. 在使用js时,我们都知道一下这样 ...
- 读 zepto 源码之工具函数
对角另一面 读 zepto 源码之工具函数 Zepto 提供了丰富的工具函数,下面来一一解读. 源码版本 本文阅读的源码为 zepto1.2.0 $.extend $.extend 方法可以用来扩展目 ...
- 决策边界绘制函数plot_decision_boundary()和plt.contourf函数详解
在做吴恩达老师的深度学习课程作业时,发现决策边界函数不好理解plot_decision_boundary(model , X , y).将此函数理解记录下: 作业地址:https://blog.csd ...
- Focus事件与blur事件
一.简介 Focus事件.当元素获得焦点时,触发focus事件.当通过鼠标点击或选中元素或通过tab键定位到元素时,该元素就会获得焦点.Focus()方法触发focus事件,或规定当发生focus事件 ...
- javascript判断元素是否已经focus
focus()函数作用 focus()是获得焦点事件. 当一个文本框获得焦点时,它里面的文本就像"百度一下"首页上的百度搜索输入框那样会被自动选中,这样的操作可以利用onfocus ...
- ABAP 外币金额转人民币金额函数 CONVERT_TO_LOCAL_CURRENCY
导语:最近在做外币转人民币的报表,记录一下. 转换函数[CONVERT_TO_LOCAL_CURRENCY] 外币转人民币,或者人民币转外币,需要业务顾问提前在OB08中做好配置才可以,因为汇率是实时 ...
最新文章
- Listview 的顶部动态广告位的实现
- java 接口表单提交_Java http 调用接口提交表单以及文件
- 项目实战Git团队操作_图形化版本
- java数组怎么倒循环_java – 用于数组倒计时的反向循环
- 在Mac OS X上安装Oracle客户端
- advisor matlab,advisor在matlab
- jsp 中的时间格式化
- 7 学大厂,拓展基础组件封装思路 BAT?TMD
- Tensorflow关于Dataset的一般操作
- Reachability的用法 判断用户的网络状态
- PHP - 简单获取页面内容
- JLink JTAG和SWD模式引脚定义
- C++ 遍历json文件
- laravel项目出现Non-static method Redis::hGet() cannot be called statically的解决方法
- 11年艺术学习“转投”数学,他出版首本TensorFlow中文教材,成为蚂蚁金服技术大军一员
- 资格考试_第三章_证券投资基金的类型
- 华为手机更新EIMU10之后google play 商店消失
- 2.模仿小米通讯录的快速索引demo
- 豪华金色粒子动态logo展示AE视频模板
- android适配器报空指针错误,适配器总是出现空指针问题