Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Automatic parameters in JavaScript

Featured Replies

I absolutely love the fact that this works. It allows me a very small amount of browser detection with only a couple of lines of code. My problem is that I don't understand why it works, and I don't like writing JavaScript I don't understand. My functions seem to have a parameter passed to them automatically. Let me show you what I mean.

 

Consider a basic form on a web page:

window.onload = function() {
var theForms = document.forms;
for (var i = 0, i_l = theForms.length; i < i_l; i++) {
	var theInputs = theForms[i].elements;
	for (var j = 0, j_l = theInputs.length; i < j_l; j++) {
		if (window.addEventListener) {
			document.getElementById(theInputs[j].id).addEventListener('focus', applyClick, false);
		} else {
			document.getElementById(theInputs[j].id).attachEvent('onfocus', applyClick);
		}
	}
}
};

We write applyClick to fill the input with a value to demonstrate it works. We define a parameter that the function is looking for, and check against it to change the value we add.

 

function applyClick(evt) {
if (evt.target) {
	var theResult = "";
} else if (evt.srcElement) {
	theResult = "o_0";
} else {
	theResult = "!_!";
}
document.getElementById("showResult").value = theResult;
}

Any browser that understands the W3C DOM will now display "^_^" when the input is focused. Microsoft Internet Explorer will display "o_0" and any other browser will display "!_!".

However, addEventListener and attachEvent don't allow parameters to be passed to functions. Logically, since "evt" is undefined, theResult should always be "!_!". Why is this not the case?

 

If you would like to see the complete script, I have uploaded it to http://www.sk80.co.uk/random_images/auto.html

Basically, you're defining a parameter in the event listener to reference the event object.

This explains it far better than I ever could.

  • Author
Basically, you're defining a parameter in the event listener to reference the event object.

This explains it far better than I ever could.

Wow... I knew there had to be a way for a script to remember how it was called -- I didn't realise I'd stumbled upon it.

And so many other things I can do with it too... was the control key pressed? where was it clicked? Thank you for this :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.