sábado, 25 de julio de 2015

4 Funciones de Javascript para ahorrarnos un dolor de cabeza

Detectar cuando el usuario oprime el botón de salida o alt + f4

function checkBrowser() {
 if(window.event.clientX < 0 && window.event.clientY < 0) { 
window.open("somefile.html", "closewindow",'left=12000,top=1200,width=120,height=50'); 
 }
}


Detectar si es un dispositovo movil y que tipo de sistema operativo usa 

var isMobile = {
  Android: function() {
    return navigator.userAgent.match(/Android/i);
  },
  BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
  },
  iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
  },
  Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
  },
  Windows: function() {
    return navigator.userAgent.match(/IEMobile/i);
  },
  any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
  }
};

// Ejemplos
if( isMobile.any() ) alert('Mobile');
if( isMobile.iOS() ) alert('iOS Device');


Obtener las Dimensiones de la pantalla

function report() { 
  document.getElementsByTagName('output')[0].innerHTML = 'screen.width:'+screen.width+'<br>screen.height:'+screen.height+'<br>window.innerWidth:'+window.innerWidth+'<br>window.innerHeight:'+window.innerHeight+'<br>window.outerWidth:'+window.outerWidth+'<br>window.outerHeight:'+window.outerHeight+'<br>document.documentElement.<br> clientWidth:'+document.documentElement.clientWidth+'<br>document.documentElement.<br> clientHeight:'+document.documentElement.clientHeight+'<br>window.devicePixelRatio:'+window.devicePixelRatio; 
}
window.addEventListener('load', report, false);
window.addEventListener('resize', report, false);
window.addEventListener('orientationchange', report, false);
window.addEventListener('deviceorientation', report, false);
window.addEventListener('MozOrientation', report, false);

Obtener valores de la URL

function getUrlParam(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return (results && results[1]) || undefined;
}

No hay comentarios.:

Publicar un comentario