prototype中的window.location.href问题

本文介绍了一种解决在Internet Explorer浏览器中使用JavaScript进行页面跳转时出现的异常返回问题的方法。通过调用Event.stop(e)并在跳转后return,有效避免了IE特有的跳转后回退现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到这样一个问题,想到通过按键进行页面跳转,因为跳转的URL是由script动态生成的。
大约是Event.observe(XXX,'click',function(e){
....
var url=.....
window.location.href=url;
}
这样的写法在FF下没有问题,但在IE下比较奇怪,好像先转到了URL,后又被转了回来。经过反复试验,想起在1.6的那本书中的一方法,先要调用Event.stop(e);跳转后return;这样解决了这个问题。
这方面我觉得 是IE的问题。
const largeObjectArray = this.createLargeObjectArray(); const newWatch = (callback) =>{ // table 打印时间 const tablePrintTime = this.calculateTime(() => { console.table(largeObjectArray); }); // 普通输出时间 const printLogTime = this.calculateTime(() => { console.log(largeObjectArray); }); // 有效时间检测(排除0值) if (tablePrintTime > 0 && printLogTime > 0) { // 动态阈值判断(当前值对比) if (tablePrintTime > printLogTime * 10) { console.log('时间对比新', tablePrintTime, printLogTime); callback(true); return; } } callback(false); } DisDev({ interval: 300, // 延长检测间隔至300ms,降低误报率 disableIframeParents: false, // 关闭iframe检测 ondevtoolopen(type, next) { if ( document.body.innerHTML.indexOf('检测到非法调试,即将关闭此页面') < 0 ) { newWatch((isDetected) => { console.log('是什么', isDetected) if (isDetected) { document.body.innerHTML = '检测到非法调试,即将关闭此页面!!'; alert('检测到非法调试,即将关闭此页面!!'); window.location.href = 'about:blank'; if (window.history.replaceState) { window.history.replaceState( null, '', window.location.pathname + window.location.search, ); } // next(); } }); } }, }); // 添加监听器 addListener(function(isOpen) { if (isOpen) { if ( document.body.innerHTML.indexOf('检测到非法调试,即将关闭此页面') < 0 ) { newWatch((isDetected) => { console.log('是什么', isDetected) if (isDetected) { document.body.innerHTML = '检测到非法调试,即将关闭此页面!'; alert('检测到非法调试,即将关闭此页面!'); window.location.href = 'about:blank'; if (window.history.replaceState) { window.history.replaceState( null, '', window.location.pathname + window.location.search, ); } } }); } } }); // 开始检测 launch(); // 禁用开发者工具 createLargeObject() { if (!this._largeObject) { this._largeObject = Array.from({length: 500}, (_, i) => ({ key: i, value: `${i}` }) ); } return this._largeObject; } createLargeObjectArray() { return Array(50).fill(this.createLargeObject()); } calculateTime(func) { const start = performance.now(); func(); return performance.now() - start; }将以上代码兼容ie浏览器,并将优化后的代码完整展示
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值