admin管理员组文章数量:1356278
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
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
Share Improve this question edited Dec 8, 2010 at 13:06 Gumbo 656k112 gold badges791 silver badges851 bronze badges asked Dec 8, 2010 at 13:05 adamadam 311 gold badge1 silver badge2 bronze badges 7- If that's the case what do you expect us to do? Your only choice is to put the JS code in the main body. Tell us what language is your server side code, post your current code and we'll try to guide you through this. – user447356 Commented Dec 8, 2010 at 13:31
- its not possible to put all the functions into the main page as some functions contain dynamic content dependant on for example session data which is checked when the div content is loaded. – adam Commented Dec 8, 2010 at 15:47
- see gymadvisory.co.uk press Find Gyms it will probably display an unstyled page - this is because the button element has no type="button" attribute yet and because the jquery assigned click function tries to call javascript functions that it can't find it does a submit - adding the missing attribute will stop that but then pressing the button does nothing – adam Commented Dec 8, 2010 at 15:52
- also - once the unstyled page is displayed press back button and try again - it now should work ! not sure why the page gone back to should work – adam Commented Dec 8, 2010 at 15:54
- reverted back to previous version of app and seems ok so must be bug in javascript changes albeit still strange behaviour. – adam Commented Dec 8, 2010 at 16:22
1 Answer
Reset to default 4Javascript 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/api/):
$(divid).innerHTML.evalScripts();
Where divid is the ID of the div you just populated with content.
本文标签: javascriptsafari ReferenceError Can39t find variableStack Overflow
版权声明:本文标题:javascript - safari ReferenceError: Can't find variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744006879a2574862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论