Jump to content

need help in javascript code.


doforumda

Recommended Posts

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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P)

 

Even without jQuery, it's just

 

<script type="text/javascript">
var obj = { name : 'sparta' };
document.onclick = function() { alert('THIS. IS. '+ obj.name.toUpperCase() +'.'); }
</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.