有时候想在别人打开网站f12调试控制台时输出点东西,那么可以这样判断:
if (window.console&&window.console.log) { //打开了控制台 console.log('控制台打开了') }
比如自动生成一个表格:
if (window.console&&window.console.log) { const data = []; for (let i = 0; i < 1000; i++) { data[i] = { title: '今天天气真好啊!', ask: '你想干嘛呢?', userAgent: navigator.userAgent, date: new Date(), host: window.location.host, width: window.innerWidth, height: window.innerHeight, port: window.location.port, url: window.location.href, ip: window.location.hostname, }; } console.table(data); }
效果: