[英]JQuery set browser title on blur to be working widthout focus
Good day, 美好的一天,
With the help of a piece of code found on stackoverflow I managed to piece together my own version of the on blur title change script. 借助于在stackoverflow上找到的一段代码,我设法将自己的on模糊标题更改脚本版本组合在一起。
The only problem I am facing is when I am loading the page and I do not click anywhere on the page itself {hence I do not 'focus'} the script does not work. 我面临的唯一问题是,当我加载页面时,我没有单击页面本身上的任何位置{因此,我没有'聚焦'}脚本不起作用。
I added a piece of code where I duplicated the focus method and changed it to load. 我添加了一段代码,在其中复制了focus方法并将其更改为加载。
$(function () {
var message = '( ! ) :+Do not forget your reservation+:';
var original = $('title').text();
$(window).load(function () {
console.log('window focussed');
if (original) {
document.title = original;
}
}).focus(function () {
console.log('window focussed');
if (original) {
document.title = original;
}
}).blur(function () {
console.log('window blurred');
var title = $('title').text();
if (title != message) {
original = title;
}
document.title = message;
});
console.log('current title : ' + original);
});
However this does not resolve my issue totally. 但是,这不能完全解决我的问题。 At this point I need click on another tab, go back and again click on another tab.
此时,我需要单击另一个选项卡,返回并再次单击另一个选项卡。 So this is pretty pointless.
因此,这毫无意义。
What am I missing to make this script work right from the start? 使该脚本从一开始就起作用的我缺少什么?
solution Many thanks to : @Brian 解决方案非常感谢:@Brian
Adding a trailing focus();
添加尾随
focus();
did the trick. 做到了。 ( And not the one from Ford.. which is great but does not work here )
( 不是福特的..那很棒,但在这里不起作用 )
Add a .focus()
call to the end of the chain to force a focus
event on load: 在链的末尾添加
.focus()
调用,以在加载时强制执行focus
事件:
$(function () {
var message = '( ! ) :+Do not forget your reservation+:';
var original = $('title').text();
$(window).load(function () {
console.log('window focussed');
if (original) {
document.title = original;
}
})
.focus(function () {
console.log('window focussed');
if (original) {
document.title = original;
}
})
.blur(function () {
console.log('window blurred');
var title = $('title').text();
if (title != message) {
original = title;
}
document.title = message;
})
.focus(); // add this line
console.log('current title : ' + original);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.