Archive

Archive for the ‘JavaScript’ Category

Replace commas on string JavaScript

June 14, 2016 Leave a comment

var mystring = "This is a string, with commas, or, is it?";
var result = mystring.replace(/,/g , "|");

Categories: Beyond, JavaScript, Mini-tips

Trigger select element (dropdown) in mobile browser, by clicking other element

May 19, 2014 Leave a comment

$("#click-this").click(function(){
    if(document.createEvent){
            var ele,evento;
		    evento= document.createEvent('MouseEvents');
		    evento.initMouseEvent(
				'mousedown',
				true,true,window,
                		0,0,0,0,0,
				false,false,false,false,
				0,null
			);
               
			ele = document.querySelector("#show-dropdown-elements");
            ele.dispatchEvent(evento);
        }
});

Javascript interview questions

November 20, 2013 Leave a comment

*how does “this” works in JavaScript?
– The this keyword alwasy refer to the owner of the function, or to the object that a function s a method of.
for instance: if you are using something like
window.function = myFunction(){
this.style.background = “#FFF”;
};

this is the window object;

and if you are doing something like

jQuery(“#myDiv”).click(function(){
var myId = this.id;
});

this is the dom element which have the id=myDiv

*3 ways to decrease page load
– optimize: images, html, css, file concatenation(css Sprites, css sheets), file minification(gif, jpg, png, css optimized, js optimizer), cache page if not changing.
– minimize http request, js, css files.
– resize images.

*Why is it better to serve site assets from multiple domains?
Because some web browser have a limit request to hosts, chrome 6, ff & IE 8+ have 8, IE7 2.

*What is FOUC?
Flash of unstyled content… when you see pieces of content without style while finishing loading the page
– you avoid this setting some class to hide the element and changing the class of the element with js, and when document ready show the element

*what does a doctype do?
To validate an HMTL, and because browsers have diff rendering modes, clarifies what version of the markup language is the page written in,
this ensures that a browser makes the best-effort attempt to following the relevant specifications, //without this the browser runs in quirks mode.

*What is the difference between == and ===?
he identity (===) operator behaves identically to the equality (==) operator except no type conversion is done,
and the types must be the same to be considered equal.

*hashtable in js?
like an associative array, but u the key can be an object, which use the hash generated of the object.

*get parameters from url in js?
– you get the location.search and split it by the question mark, and then find the variable you are looking for, and obtaining the value by spliting by the equal “=” symbol.

*Can you get post parameters in js?
you can only get uri parameters

*what is the most common use of anonymus functions?
as arguments to other functions, exemple jquery handling events.

*what is a closure in js?
when you define a function inside another function to be created or executed when the other function is being used… //dubious

*What are undefined and undeclared variables?
undefined = variables that dont have an assigned value yet.
undeclared = variables that are not part of your code, not declared in the scope…

* What are the diff between attribute and property?
html = attribute
DOM = property

*Differences between native and hosts Objects?
Native objects = Object, Function, String, Date, Math
Host Objects = the enviroment objects(window, document, location, history…)

Categories: Entrevistas, JavaScript

Problema con evento “scroll” de jquery en IE8

May 9, 2013 Leave a comment

Usando jQuery en IE8, tuve un problema cuando quería disparar el evento de “scroll” al top de la página, entonces me di cuenta que del error en IE8 con scroll y document:

$(document).scroll(); //no funciona
$(window).scroll(); //sí funciona.

Categories: Browsers, IE8, JavaScript, jQuery

Remove duplicated elements, Array JavaScript

March 20, 2013 1 comment

I got this piece of code from stackoverflow, and im just sharing this cus this is something that I am usually looking for :P and this will save some time :), I am adding the source if you want to check out more options on how to do this.


function getDistinctArray(a) {
var d = {};
return a.filter(function(e) {
var h = e.valueOf(), i = d[h];
d[h] = true;
return !i;
});
}

Source

Obtener valor(option) de elemento select al cambiar – jQuery

August 5, 2011 11 comments

HTML de ejemplo

 <select id="mySelect">
    <option value="1">option A</option>
    <option value="2">option B</option>
</select>
<br/>
<span id="iSelected">Hola</span>
 

jQuery para conseguir el option recien seleccionado:

 
$('#mySelect').change(function(){
    var $selectedOption = $(this).find('option:selected');
    var selectedLabel = $selectedOption.text();
    var selectedValue = $selectedOption.val();
    $('#iSelected').text(selectedValue + ' - ' + selectedLabel);
});

JsFiddle con el ejemplo

Cambiar el width de un input dependiendo de su entrada

July 20, 2011 Leave a comment

Hoy estaba mirando por la ventana, cuando repentinamente se me vino a la mente, jumm… por que no incrementar el ancho de un input dependiendo de la longitud de lo que se está escribiendo… entonces llegue a esto:

<input 
    onkeyup="this.style.width = this.value.length + 2 + 'ex';" 
    onkeypress="this.onkeyup();"/>

Es importante también definir en tu CSS los tamaños máximos y mínimos del input, para que no desaparesca ni tampoco cresca hasta el infinito de los infinitos

input{ min-width: 100px; max-width: 50ex;}

Y… pueden ver el ejemplo en jsfiddle