Solved

Function not working inside even listener script

Posted on 2011-02-18
2
200 Views
Last Modified: 2012-08-13
I found a great listener script that listens for the onResize method, but I'm trying to pass another function inside this function that will update the document.title object and display the window's width.  My problem is, I'm getting an undefined on my change() function.  Any ideas?
function change() {
		document.title = wWidth;
	}
	function attachEventHandler(element, eventToHandle, eventHandler) {
		if(element.attachEvent) {
			element.attachEvent(eventToHandle, eventHandler);
		} else if(element.addEventListener) {
			element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
		} else {
			element[eventToHandle] = eventHandler;
	  	}
	}
	attachEventHandler(window, "onresize", function() {
		setTimeout("change()", 100);
		if(wWidth > 1000) {
			document.body.style.overflowY = "hidden";
		} else if(wWidth <= 1000) {
			document.body.style.overflowY = "scroll";
		}
	});

Open in new window

0
Comment
Question by:pingeyeg
2 Comments
 
LVL 4

Expert Comment

by:LAMASE
ID: 34930759
try with a dummy

var wWidth;

Open in new window


outside (and before) every function. This will make the variable "global"

http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34931774

Try this instead:

function change() {
	document.title = wWidth;
}

function attachEventHandler(element, eventToHandle, eventHandler) {
	if(element.attachEvent) {
		element.attachEvent(eventToHandle, eventHandler);
	} else if(element.addEventListener) {
		element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
	} else {
		element[eventToHandle] = eventHandler;
  	}
}

attachEventHandler(window, "onresize", function() {
	window.wWidth = document.documentElement.clientWidth;
	setTimeout(change, 100);
	if(wWidth > 1000) {
		document.body.style.overflowY = "hidden";
	} else if(wWidth <= 1000) {
		document.body.style.overflowY = "scroll";
	}
});

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now