私は JavaScript の初心者です。なぜこのコードが頭に書かれたときに機能するのかを理解しようとしていますが、外部ファイルから参照されているときはそうではありません。
私のhtmlドキュメントの先頭で、次のようにjavascriptファイル「quote.js」を参照しています。
<script type="text/javascript" language="JavaScript" src="/js/quote.js"> </script>
quote.js の中身は以下の通り
var textarray = [
"Be Good.",
"Our future depends powerfully on how well we understand the cosmos.",
"Bottomless wonders spring from simple rules... repeated without end.",
"All our science, measured against reality, is primitive and childlike — and yet, it is the most precious thing we have.",
"To use violence is to already be defeated."
];
function RndText() {
var rannum= Math.floor(Math.random()*textarray.length);
document.getElementById('ShowText').innerHTML=textarray[rannum];
}
window.onload = function() { RndText(); }
最後に、ボディで置き換えているdivは次のとおりです...
<div id = "ShowText"></div>
それはおそらくばかげた間違いですが、私はしばらくの間それを追跡しようとしてきましたが、何かが欠けています. quote.js の内容を html の head に書いてみると、うまくいきます。何か案は?前もって感謝します。