doforumda Posted July 24, 2012 Share Posted July 24, 2012 I have a javascript code and want to extend this code now. how can I write a makeAMessenger function in global scope so that it triggers when user clicks document and alert THIS. IS. SPART. currently I have following code. function initiateMadness() { var sparta = { name : "Sparta" }; function madness() { alert("THIS. IS. " + this.name.toUpperCase() + "."); } document.onclick = makeAMessenger(madness, sparta); } initiateMadness(); Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/ Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 jQuery FTW! var foo = { name: 'sparta' }; $(document).live('click', function(){ alert('THIS. IS. ' + foo.name); }); Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1363950 Share on other sites More sharing options...
doforumda Posted July 24, 2012 Author Share Posted July 24, 2012 I want to use the same code which I have in my original thread. I want to extend it. Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1363959 Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 I'd also use jQuery because it's so much easier in the long run. If you don't want to do that, you need to look at the onClick() event in pure JS. Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1364019 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 Loading an entire library to do this? It's MADNESS I TELL YOU! I don't see the issue exactly. If you want a make a function available to the global scope, just define it in the global scope :/ I'm not sure why/what you're trying to do, but I think this accomplishes what you want <script type="text/javascript"> function doSomething( func, obj ) { func(obj.name); } function init() { var innerObj = { name : 'object' }; var innerFunc = function( string ) { alert( 'THIS. IS. '+ string.toUpperCase() +'.' ) } document.onclick = function() { doSomething( innerFunc, innerObj ); } } init(); </script> There are MUCH easier ways to do this, though. Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1364046 Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 You know he's going to do more later on :-P Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1364047 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 Yeah, but jQuery is just so bloated. I wouldn't use it at all if I wasn't sure almost everyone has it cached anyways (I default to Google's hosted version ) Even without jQuery, it's just <script type="text/javascript"> var obj = { name : 'sparta' }; document.onclick = function() { alert('THIS. IS. '+ obj.name.toUpperCase() +'.'); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/#findComment-1364052 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.