Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal, tenho o código abaixo e queria melhorar ele, para se receber nulo/undefined, vazio (=""), colocar um valor default, está funcionando, só falta adicionar o vazio, tentei do jeito abaixo e não funcionou:
var h = $(this).attr("height") != null || $(this).attr("height") != "" ? h = $(this).attr("height") : h = 500;
var w = $(this).attr("width") != null || $(this).attr("width") != "" ? w = $(this).attr("width") : w = 600;>
22 minutos atrás, William Bruno disse:
Em javascript tanto null quanto undefined ou string vazia são evaluados para false. Portanto, basta você fazer:
var h = $(this).attr("height") || 500;
var w = $(this).attr("width") || 600;
Olá William, não sei pq essa solução não adiantou...
Fica zerada, é que são atributos dinâmicos e as vezes não terá valores e ficará apenas height ou width sem nem o height="".
Para estes casos eu não sei como resolverPosta um mínimo de código para reproduzirmos a situação.
>
7 minutos atrás, William Bruno disse:
Posta um mínimo de código para reproduzirmos a situação.
Foi mal...
//Exibe Popup do link - F0124186
$("body").on('click','.openPopup',function(e) {
/* var params = '[%xpath://sec/@sessionURL%]'; AQUI É O OUTRO TÓPICO, NÃO SEI PEGAR OS VALORES DESSE XPATH*/
console.log(params);
var h = $(this).attr("height") != null ? h = $(this).attr("height") : h = 500;
var w = $(this).attr("width") != null ? w = $(this).attr("width") : w = 600;
console.log(h, w);
var leftW = ($(window).width() - w) / 2;
var topH = ($(window).height() - h) / 2;
console.log(leftW,topH);
var form = null;
if($(this).is(".submitForm")) {
form = $(this).closest("form");
} else {
form = $("<form />");
}
form.attr("action",$(this).attr("href"));
form.attr("target","pop1");
params = params.split("&");
for(var p = 0; p < params.length; p++) {
var hd = $("<input type='hidden' name='" + params[p].split("=")[0] + "' value='"+params[p].split("=")[1]+"' />");
hd.appendTo(form);
}
form.attr("method","POST");
window.open($(this).attr("href"),"pop1","width="+ w +",height="+ h +",left=" + leftW + ",top=" + topH);
form.submit();
e.stopPropagation();
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
});Nada?
Em javascript tanto null quanto undefined ou string vazia são evaluados para false. Portanto, basta você fazer: