Why can't I call this object's function from an in-line anchor?
-
24-10-2019 - |
Question
HTML:
<a href="javascript:alert('Hey.')">Alert</a><br/>
<a href="javascript:alert('In anchor:\n' + objection.sustained());">Alert</a>
Javascript:
var objection = {
sustained : (function() {return ("accroches-toi a ton reve")})
};
alert("In script: \n" + objection.sustained());
$("outdat").text( +"<br/>\n");
Just curious here, why can't I call objection.sustained()
from an anchor tag, but it's OK to do it from the script region?
It seems to not even know the object exists when using the anchor. Happens in several major browsers so I believe this is by design?
Solution
Variable scope.
Fiddle is placing the variable in the document load scope so you're anchor code can not see it. As @kjy112 mentioned, remove this from those code blocks and all should work fine.
OTHER TIPS
Use No Wrapper(head) or No Wrapper(body) on JSFiddle
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow