2016-09-20 66 views
0

我將時間戳添加到JS和CSS標記。如果文件被改變,它允許總是有最新的代碼。但是,它不允許在開發人員控制檯中調試JS。使用document.writeln添加腳本不允許在開發人員工具中調試

的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

如果沒有定義的變量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>

+0

bDeveloperMode是不是一個問題。它是其他地方定義的全局變量。問題是Chrome中的調試器不允許在JS腳本添加了document.writeln和timestamp的情況下添加斷點。我的問題是有可能有不同的方式來導入用於開發與生產的JS腳本。 – kaplievabell

+0

我剛剛檢查過,調試這個沒有問題。你究竟如何調試它? – Dekel

相關問題