只是想知道谷歌浏览器是否会在某个时候支持 window.focus()。当我说支持时,我的意思是让它发挥作用。对它的调用不会失败,它只是什么都不做。所有其他主流浏览器都没有这个问题:FireFox、IE6-IE8 和 Safari。
我有一个用于管理浏览器窗口的客户端类。当我第一次创建一个窗口时,该窗口进入焦点,但随后尝试将焦点置于该窗口时却不起作用。
据我所知,这似乎是一种避免烦人的弹出窗口的安全功能,而且它似乎不是 WebKit 问题,因为它在 Safari 中有效。
我知道有人提出的一个想法是关闭窗口然后重新打开它,但这是一个糟糕的解决方案。谷歌搜索显示我似乎不是唯一对此感到沮丧的人。
为了 100% 清楚,我的意思是新窗口,而不是选项卡(选项卡不能从我读过的内容中聚焦)并且所有打开的窗口都在同一个域中。
除了我上面提到的不好的想法之外,还有什么想法和解决方法吗?
Chromium 项目上记录了一个关于此的错误,请查看 here .感谢您发布Rich .
MyCompany = { UI: {} }; // Put this here if you want to test the code. I create these namespaces elsewhere in code.
MyCompany.UI.Window = new function() {
// Private fields
var that = this;
var windowHandles = {};
// Public Members
this.windowExists = function(windowTarget) {
return windowTarget && windowHandles[windowTarget] && !windowHandles[windowTarget].closed;
}
this.open = function(url, windowTarget, windowProperties) {
// See if we have a window handle and if it's closed or not.
if (that.windowExists(windowTarget)) {
// We still have our window object so let's check if the URLs is the same as the one we're trying to load.
var currentLocation = windowHandles[windowTarget].location;
if (
(
/^http(?:s?):/.test(url) && currentLocation.href !== url
)
||
(
// This check is required because the URL might be the same, but absolute,
// e.g. /Default.aspx ... instead of http://localhost/Default.aspx ...
!/^http(?:s?):/.test(url) &&
(currentLocation.pathname + currentLocation.search + currentLocation.hash) !== url
)
) {
// Not the same URL, so load the new one.
windowHandles[windowTarget].location = url;
}
// Give focus to the window. This works in IE 6/7/8, FireFox, Safari but not Chrome.
// Well in Chrome it works the first time, but subsequent focus attempts fail,. I believe this is a security feature in Chrome to avoid annoying popups.
windowHandles[windowTarget].focus();
}
else
{
// Need to do this so that tabbed browsers (pretty much all browsers except IE6) actually open a new window
// as opposed to a tab. By specifying at least one window property, we're guaranteed to have a new window created instead
// of a tab.
windowProperties = windowProperties || 'menubar=yes,location=yes,width=700, height=400, scrollbars=yes, resizable= yes';
windowTarget = windowTarget || "_blank";
// Create a new window.
var windowHandle = windowProperties ? window.open(url, windowTarget, windowProperties) : window.open(url, windowTarget);
if (null === windowHandle) {
alert("You have a popup blocker enabled. Please allow popups for " + location.protocol + "//" + location.host);
}
else {
if ("_blank" !== windowTarget) {
// Store the window handle for reuse if a handle was specified.
windowHandles[windowTarget] = windowHandle;
windowHandles[windowTarget].focus();
}
}
}
}
}
最佳答案
我一直在努力解决这个问题。我想要对另一个窗口的引用,所以我发布了:
otherWinRef = window.open("","OtherWindow");
但是,当我发出此命令时,浏览器会将焦点切换到 OtherWindow。我认为这可以通过这样做来解决:
otherWinRef = window.open("","OtherWindow");
window.focus();
但是 window.focus() 没有效果。我试过:
otherWinRef = window.open("","OtherWindow");
setTimeout(window.focus,0);
但是 window.focus() 调用仍然没有效果。
我通过将以下代码添加到 OtherWindow 的源代码来解决这个问题。
function Bounce(w) {
window.blur();
w.focus();
}
然后我将主窗口中的代码更改为:
otherWinRef = window.open("","OtherWindow");
otherWinRef.Bounce(window);
关于javascript - window.focus() 在谷歌浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2758608/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我在目录“C:\DocumentsandSettings\test.exe”中有一个文件,但是当我用单引号编写命令时`C:\DocumentsandSettings\test.exe(我无法在此框中显示),用于在Ruby中执行命令,我无法这样做,我收到的错误是找不到文件或目录。我尝试用“//”和“\”替换“\”,但似乎没有任何效果。我也使用过系统、IO.popen和exec命令,但所有的努力都是徒劳的。exec命令还使程序退出,这是我不想发生的。提前致谢。 最佳答案 反引号环境就像双引号,所以反斜杠用于转义。此外,Ruby会将空格解
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw