Javascript在Grails中不起作用

时间:2011-07-19 18:28:24

标签: javascript grails gsp

我有一个简单的脚本在html中工作正常。然而在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');
}

当我加入gsp视图页面时,我将<g:javascript src="getKey.js" />放在<head></head>之间。

然后我调用函数initAll(),如<body onload="initAll()">,其他像<div class="holder"><input type="button" class="buttonstyle" value="7" name="Seven" onclick="parseNumber(7)" /></div>

谁能告诉我什么是错的?我确信脚本已被正确包含,因为警告“hello22”被扔了。

1 个答案:

答案 0 :(得分:1)

尝试将代码放在页脚的某处:

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

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

而不是<body onload="initAll"