Solved

Function not working inside even listener script

Posted on 2011-02-18
2
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…
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…

730 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