开发者

safari ReferenceError: Can't find variable

I have the following in its own DIV

<script>
function newfunc()
{
alert("here");
}
</script>
<button type="button" onclick="newfunc()">Press me</button>

On IE and FF it works fine, but on Safari (at least Safari 4.0.5) it says “ReferenceError can't find variable” - now the content was loaded into the DIV dynamically and it seems Safari can't see the function definitions within this DIV - however, if I put the function newfunc() onto the main HTML page (i.开发者_运维技巧e. not in the DIV) then the button press does call the function.

It’s as if Safari doesn't see the Javascript functions that have been added to the DIV.

Any help would be appreciated.

adam


Javascript that is added dynamically to a page needs to be eval-d in order to correctly initialise.

In order for that to work correctly for function definitions they need to be formatted slightly differently. eg:

<script>
newfunc = function()
{
alert("here");
}
</script>
<button type="button" onclick="newfunc()">Press me</button>

After adding the content to the DIV, extract all tag elements from the DIV and eval their contents.

For example, using PrototypeJS (http://www.prototypejs.org/api/):

$(divid).innerHTML.evalScripts();

Where divid is the ID of the div you just populated with content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜