2011-07-19 21 views
0

私は簡単なスクリプトがhtmlでうまく動作します。しかし、gspでは、もう動作しません。JavascriptがGrailsで動作しない

私のスクリプトは以下のように、非常に簡単です:私はGSPビューページに含ま

// program variables 
var displayElement; 

// constants 
var maxLength = 300; 

// calculator variables 
var text; 
var currentLength; 
var dotPosition; 

var lastNumber; 
var currentNumber; 
var rememberedNumber; 
var operator; 

alert('hello22'); 

function parseNumber(number) { 
     if ((number == -1) && (dotPosition > 0) || (maxLength == currentLength)) return; 
     if (currentLength == 0) text = ''; 
     currentLength++; 
     if (number == -1) { 
       text += '.'; 
       dotPosition = currentLength; 
     } else text += number; 

     displayElement.value = text; 
} 

function parseBackspace() { 
     if (currentLength == 0) return; 
     if ('.' == text[currentLength-1]) dotPosition = 0; 
     text = text.slice(0, currentLength-1); 
     if(--currentLength == 0) text = ''; 
     displayElement.value = text; 
} 

function parseClearEntry() { 
     currentLength = 0; 
     text = '0'; 
     dotPosition = 0; 
     displayElement.value = text; 
} 

function parseClear() { 
     parseClearEntry(); 
     lastNumber = 0; 
     operator = ''; 
     //added by Kevin 
     displayElement.value = ''; 
} 

function initAll() { 
     alert('hello1113333333'); 
     text = '0'; 
     currentNumber = 0; 
     currentLength = 0; 
     displayElement = document.getElementById('TextBox'); 
     rememberedNumber = 0; 
     lastNumber = 0; 
     dotPosition = 0; 
     alert('hello1111111111'); 
} 

が、私は<head></head><g:javascript src="getKey.js" />を置きます。

は、その後、私は<body onload="initAll()">ようinitAll()関数を呼び出し、そして<div class="holder"><input type="button" class="buttonstyle" value="7" name="Seven" onclick="parseNumber(7)" /></div>

のような他の人は誰もが間違って何を教えてもらえますか?警告 "hello22"が投げられたので、スクリプトが正しく組み込まれていると確信しています。どこかフッターに次のコードを配置する

+0

あなたは、おそらくどのように見えるか、物事を参照して、「ソースの表示」を行う必要がありますページがブラウザに表示されたとき。また、あなたが意味することを正確に説明したいかもしれません。奇妙な効果?間違った効果?不安定な行動ですか? – Pointy

答えて

1

試してみてください。

<g:script> 
(function() { initAll(); })(); 
</g:script> 

または

<g:script> 
window.onload = initAll; //note that is's without braces 
</g:script> 

代わりの<body onload="initAll"

+2

私は何が間違っているのか分かっています...レイアウトはすべてを置き換えます...それはレイアウトに入れた後に機能します。ありがとう!! – Kevin

関連する問題