?
Solved

Function not working inside even listener script

Posted on 2011-02-18
2
Medium Priority
?
209 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

770 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