Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function not working inside even listener script

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

609 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