admin管理员组文章数量:1289543
I have two different JSPs that the Java backend concatenates together and sends back to the same rendered HTML page.
Each JSP has its own <script>
block and defines functions inside that block:
JSP #1:
<script type="text/javascript">
function blah() { ... }
</script>
JSP #2
<script type="text/javascript">
function foo()
{
blah();
}
</script>
Like I said, the backend adds these to the HTTP response and sends them back to the browser during the same request.
When I run this page in my browser, I can tell right away that blah()
is not executing when foo()
is getting called. I see a console error in Firebug stating blah()
is not defined. I'm wondering if blah()
only has scope inside its own <script>
tag, and likewise for foo()
. Is that the case here, or is something else awry?
When I go to view the page source I see both script blocks and both functions. This tells me everything is being generated/rendered correctly server-side, but perhaps my approach is inherently wrong (defining the functions inside different script tags). Thanks in advance.
I have two different JSPs that the Java backend concatenates together and sends back to the same rendered HTML page.
Each JSP has its own <script>
block and defines functions inside that block:
JSP #1:
<script type="text/javascript">
function blah() { ... }
</script>
JSP #2
<script type="text/javascript">
function foo()
{
blah();
}
</script>
Like I said, the backend adds these to the HTTP response and sends them back to the browser during the same request.
When I run this page in my browser, I can tell right away that blah()
is not executing when foo()
is getting called. I see a console error in Firebug stating blah()
is not defined. I'm wondering if blah()
only has scope inside its own <script>
tag, and likewise for foo()
. Is that the case here, or is something else awry?
When I go to view the page source I see both script blocks and both functions. This tells me everything is being generated/rendered correctly server-side, but perhaps my approach is inherently wrong (defining the functions inside different script tags). Thanks in advance.
Share Improve this question asked Apr 23, 2012 at 23:16 IAmYourFajaIAmYourFaja 56.9k186 gold badges485 silver badges778 bronze badges 2- 2 when or how do you call foo()? – Philipp Commented Apr 23, 2012 at 23:18
- foo() is called when a user hovers their mouse over an element. I can verify that it gets called by replacing its contents with an alert("") statement. – IAmYourFaja Commented Apr 23, 2012 at 23:37
3 Answers
Reset to default 5all of them are global. they can see each other. the problem is when they get defined and call each other.
you should define and call them in this order:
- bar
- foo
- call foo
- foo executed and calls bar
- bar is executed
You can call function like this:
(function($) {
var namespace;
namespace = {
something : function() {
alert('hello there!');
},
bodyInfo : function() {
alert($('body').attr('id'));
}
};
window.ns = namespace;
})(this.jQuery);
$(function() {
ns.something();
ns.bodyInfo();
});
The only thing that defines scope in JavaScript is a function, so your problem is not a scoping issue. You most probably are not calling foo(), you call it before blah() is defined, or you have a syntax error somewhere. Maybe you can post your whole HTML page so we can see what's going on.
本文标签: htmlJavaScript function scope between script tagsStack Overflow
版权声明:本文标题:html - JavaScript function scope between script tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741390411a2376071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论