Solved

Function not working inside even listener script

Posted on 2011-02-18
2
202 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 69
Change text to radio button and calendar form 2 36
Javascript and Jquery not firing 9 42
how can i extend ? 2 6
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now