我正在为JS和CSS标签添加时间戳。如果文件被更改,它允许始终拥有最新的代码。但是,它不允许在开发人员控制台中调试JS。
的index.html
<head>
<script type="text/javascript">
//if not developer mode add timestamp to prevent caching
var bDeveloperMode=true;
var timeStamp = !bDeveloperMode ? ('?_=' + new Date()).valueOf() : '';
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript2.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript3.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript4.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript5.js' + timeStamp + '"></scr' + 'ipt>');
function myFunction1(){
console.log('break here 1');
}
function myFunction2(){
console.log('break here 2');
}
</script>
</head>
someScript.js
function myFunction3(){
console.log('break here 2');
}
脚本的顺序很重要。 反正在脚本标签中是否有时间戳并且仍能调试? 谢谢。
答案 0 :(得分:0)
如果未定义bDeveloperMode
变量,该行将抛出Uncaught ReferenceError
Uncaught ReferenceError: bDeveloperMode is not defined
其余的javascript代码将无法运行。
相反 - 您可以使用typeof并检查它是否未定义:
typeof bDeveloperMode === 'undefined'
以下是对您的代码的修复:
<head>
<script type="text/javascript">
//if not developer mode add timestamp to prevent caching
var timeStamp = typeof bDeveloperMode === 'undefined' ? ('?_=' + new Date()).valueOf() : '';
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript2.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript3.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript4.js' + timeStamp + '"></scr' + 'ipt>');
document.writeln('<scr' + 'ipt type="text/javascript" src="../js/someScript5.js' + timeStamp + '"></scr' + 'ipt>');
function myFunction1(){
console.log('break here 1');
}
function myFunction2(){
console.log('break here 2');
}
</script>
</head>