可以使用 Javascript 刷新浏览器窗口吗? [英] Possible to flash a Browser window using Javascript?

查看:35
本文介绍了可以使用 Javascript 刷新浏览器窗口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多程序一样闪烁它们在任务栏/停靠栏上的窗口以提醒用户切换到程序,

Like many programs flash their window on the taskbar / dock to alert the user to switch to the program,

是否可以使用 Javascript 刷新 浏览器 窗口?(也欢迎仅使用 FireFox 的脚本)

Is it possible to flash the Browser window using Javascript? (FireFox-only scripts are also welcome)

这对于有大量实时活动的基于网络的聊天/论坛/基于社区的软件很有用.

This is useful for web-based Chat / Forum / Community-based software where there is lots of real-time activity.

推荐答案

@Hexagon Theory: 你为什么要重写整个 head 元素只是为了改变 head 中一个元素的值?您的解决方案在多个层面上都非常低效.

@Hexagon Theory: Why would you ever rewrite the whole head element just to change the value of one element in the head? Your solution is horribly inefficient on multiple levels.

<html>
<head>
<link rel="icon" href="on.png" type="image/png" id="changeMe" />
<script type="text/javascript" src="flash.js"></script>
</head>
<body>
</body>
</html>

flash.js:

function Flasher(speed) {
  var elem = document.getElementById('changeMe');

  this.timer = setTimeout(function() {
    elem.href = elem.href ==  'on.png' ? 'off.png' : 'on.png';
  }, speed);

  this.stop = function() { clearTimeout(this.timer); }
}

/* sample usage
 *
 * var flasher = new Flasher(1000);
 * flasher.stop();
 */

它不一定是一个类,但它有助于保持全局命名空间的清洁.这是未经测试的,但如果由于某种原因简单地更改 href 不起作用,请克隆链接节点,更改 href 并将旧链接替换为克隆的链接.

It didn't really have to be a class but it helped keep the global namespace clean. That's untested but if simply changing the href doesn't work for some reason, clone the link node, change the href and replace the old link with the cloned one.

这篇关于可以使用 Javascript 刷新浏览器窗口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆