Outils pour utilisateurs

Outils du site


Panneau latéral

webdev:javascript:basics

Ceci est une ancienne révision du document !


Basics

Events

  • On Chrome e.originalEvent.screenX take different values than e.originalEvent.clientX, unlike Firefox.
Pass additionnal parameters to an event using jQuery .trigger()
// Trigger
$('.volume').on('mousemove', function(e)
{
	console.log(e);
	console.log(e.pageX);
	$('.volume').trigger('mousedown', [e.pageX] );
});

// We get the value as
$('.volume').on('mousedown', function(e, altPageX)
{
	console.log(e);
	console.log(altPageX);
});
Debouncing / Throttle

Throttle : Similar to debouncing but ensure a minimum execution of the function every fixed time

Using jQuery's .one() can also work.

Promises

Check if a variable exist in javascript without getting an error

Source

if( typeof google_step != 'undefined')
{
	// ...
}

Objects

Object copy and references

	var tmpDisable = {};
	var lastTmpDisable = {};

	lastTmpDisable = tmpDisable; // Any change made to any of these object will be on both because they're references

	// If we declare it this way a new object is created
	for(var key in tmpDisable)
	{
		lastTmpDisable[key] = tmpDisable[key]
	}

Get the length of an object (ES5)

var size = Object.keys(myObj).length;

Copying to clipboard

You can select all the text in a textarea, and also fill it with the variable you want before. The textarea need to be visible for the text to be selected, and you can't juste hide it just after.

//////////////////////////////////////
// Javascript
$(document).ready(function() 
{
	$('.copy-ids').on('click', function(e)
	{
		var to_copy = '';

		$('.id_to_copy').each(function(index)
		{	
			to_copy = to_copy + $(this).html() + ' ; ';
		});

		$('.paste-ids').html(to_copy).css('visibility', 'visible');
		$('.paste-ids')[0].select();
		document.execCommand("copy");

		// TODO : indicateur visuel
		
	});
});

//////////////////////////////////////
// HTML
<input class="copy-ids" type="button" value="Copier les ids"/>
<textarea class="paste-ids"></textarea>
webdev/javascript/basics.1544726575.txt.gz · Dernière modification: 13/12/2018 19:42 de dolo