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
https://forums.phpfreaks.com/topic/266158-need-help-in-javascript-code/
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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.