What function do you suggest when you have a lot of parameters and only one condition to pass through? I want something like this.
function deleteClaimLine(secId, aBtn, cboF, lstServ, cboServ, cboServP, txtMont, txtAnn1, txtAnn2, txtAnn3, fileUpl, dBtn) {
All variables...
if (allParameters !== "") {
allParameters.value = "";
}
}
Actually, that's all conditions i have and i want to do it in only one line if possible:
if (cboFamille !== "") {
cboFamille.value = "";
}
if (lstServices !== "") {
lstServices.value = "";
}
if (cboService !== "") {
cboService.value = "";
cboService.style.display = "none";
}
if (cboServiceP !== "") {
cboServiceP.value = "";
}
if (txtMontant !== "") {
txtMontant.value = "";
}
if (txtAnnee1 !== "") {
txtAnnee1.value = "";
}
if (txtAnnee2 !== "") {
txtAnnee2.value = "";
}
if (txtAnnee3 !== "") {
txtAnnee3.value = "";
}
if (fileUpload !== "") {
fileUpload.value = "";
}
if i got your questions correctly, follow this example it might help you
function func(...allParameters) {
if (!allParameters.includes(""))
allParameters = allParameters.map(_ => "")
}
func('ts', 'js');
Related
I'm currently working on a Firebase project and I'm stuck on data ordering.
Here's my javascript code:
function displayContent() {
if (firstKey != null) {
var i = 0;
ref.orderByKey().endAt(firstKey).limitToLast(5).on("child_added", function(childSnapshot) {
var childType;
if (i == 0) {
firstKey = null;
childType = "lastChild";
} else {
childType = "firstChild";
}
i++;
if (i < 5) {
var containerElement = document.getElementsByClassName('post-container')[0];
if (childSnapshot.val().image != null) {
containerElement.insertBefore(
createPostElementImg(
childSnapshot.key,
childSnapshot.val().title,
childSnapshot.val().desc,
childSnapshot.val().image),
containerElement.childType);
if (firstKey == null) {
firstKey = childSnapshot.key;
console.log(firstKey);
}
} else {
containerElement.insertBefore(
createPostElement(
childSnapshot.key,
childSnapshot.val().title,
childSnapshot.val().desc),
containerElement.childType);
if (firstKey == null) {
firstKey = childSnapshot.key;
console.log(firstKey);
}
}
}
});
} else {
ref.orderByKey().limitToLast(5).on("child_added", function(childSnapshot) {
var containerElement = document.getElementsByClassName('post-container')[0];
if (childSnapshot.val().image != null) {
containerElement.insertBefore(
createPostElementImg(
childSnapshot.key,
childSnapshot.val().title,
childSnapshot.val().desc,
childSnapshot.val().image),
containerElement.firstChild);
if (firstKey == null) {
firstKey = childSnapshot.key;
console.log(firstKey);
}
} else {
containerElement.insertBefore(
createPostElement(
childSnapshot.key,
childSnapshot.val().title,
childSnapshot.val().desc),
containerElement.firstChild);
if (firstKey == null) {
firstKey = childSnapshot.key;
console.log(firstKey);
}
}
});
}
}
The first call, when the firstKey is equal to null is ordered properly, but the data loaded onScroll (when the firstKey is not null) is displayed in the wrong order. Is there any way to sort that data?
Thanks!
EDIT:
I have this piece of code for the detection of the page being scrolled to the bottom:
$(document).ready(function() {
var l_post = 0;
var n_post = 0;
var ref = firebase.database().ref().child('Blog');
ref.once("value", function(snapshot) {
n_post = snapshot.numChildren();
console.log(n_post);
});
jQuery(
function($)
{
$(".content").bind('scroll', function()
{
if($(this).scrollTop() + $(this).innerHeight()==$(this)[0].scrollHeight)
{
if(l_post < n_post) {
displayContent();
l_post = l_post + 5;
console.log(l_post);
}
}
})
}
);
});
The hierarchy can be very deep, So how to check if the a property exists somewhere within the root object ? If not then create it.
I have the following html
<div id="container">
<div id="level-1">
<input id="value-1" group="a.b" name="c" value="some-value-1">
</div>
</div>
Now i would like to put the value of this input into a javascript object based on the parent attribute.
Desired output is
{
"a" : {
"b" : {
"c" : "some-value-1"
}
}
}
My Effort :
function traverse(id, obj) {
var methodName = "traverse";
var elementToTraverse = document.getElementById(id);
var currentElementLength = elementToTraverse.childNodes.length;
if (currentElementLength > 0) {
var children = getChildNodes(elementToTraverse);
for (var ch in children) {
var currentChild = children[ch];
//ignore the text nodes
if (currentChild.nodeType == 3) {
continue;
}
if (currentChild.nodeType == 1 && currentChild.childNodes.length > 0 && currentChild.id != "") {
//call without the object argument as it has already been constructed.
traverse(currentChild.id, obj);
}
else if (currentChild.nodeType == 1 && currentChild.id != "" && currentChild.getAttribute('name') != null) {
if (DEBUG) {
logOnConsole(methodName, currentChild.getAttribute('name') + "--" + currentChild.id, logLevel.INFO);
}
var group = currentChild.getAttribute('group') || null;
var name = currentChild.getAttribute('name');
var value = getValue(currentChild);
if (value == "" || value == undefined) {
if(group){
if(isNull(obj[group])){
obj[group] = new Object();
}
obj[group][name] = "";
}
else{
obj[name] = "";
}
}
else if(group){
if(isNull(obj[group])){
obj[group] = new Object();
}
obj[group][name] = value;
}
else {
obj[name] = value;
}
}
else {
if (DEBUG) {
logOnConsole(methodName, "Element not useful. \n" + currentChild.nodeName, logLevel.INFO);
}
}
}
}
return obj;
}
I call it via traverse('container-id', new Object()) but this will work for a single value in the group rather than a nested structure.
Try this
function isExist(obj, path) {
patharray = path.split(".");
for(var i=0;i < patharray.length; i++) {
obj = obj[patharray[i]];
if(obj === undefined) return false
}
return true;
}
document.body.innerHTML = isExist({subobject:{subsubobject:{test: 34}}}, 'subobject.subsubobject');
I'm trying to duplicate incident entity when click on a custom ribbon button. Everything is ok but i'm having problem with some fields. I'll share the code below.
When i remove the fields that made problem everything works fine, my web resource opens a new entity and populates fields. If i dont remove that fields im getting error. The odd situation is if i create another web resource with fields that i removed from my first Wresource it works fine too. Am i missing something or is there a field restriction ?
The code is working fine:
Comment outted section is making problem.
function test() {
if (Xrm.Page.data.entity.attributes.get("customerid").getValue() != null) {
var CustomerId = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].id;
var CustomerName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
var CustomerType = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].entityType;
}
if (Xrm.Page.data.entity.attributes.get("new_malzeme").getValue() != null) {
var MatNumbId = Xrm.Page.data.entity.attributes.get("new_malzeme").getValue()[0].id;
var MatNumbName = Xrm.Page.data.entity.attributes.get("new_malzeme").getValue()[0].name;
}
if (Xrm.Page.data.entity.attributes.get("new_satisburosu").getValue() != null) {
var SatBurId = Xrm.Page.data.entity.attributes.get("new_satisburosu").getValue()[0].id;
var SatBurName = Xrm.Page.data.entity.attributes.get("new_satisburosu").getValue()[0].name;
}
var Talep = Xrm.Page.data.entity.attributes.get("new_talep").getValue();
var ilgiliKisi = Xrm.Page.data.entity.attributes.get("new_ilgilikisi").getValue();
var UrunAdi = Xrm.Page.data.entity.attributes.get("new_urunadi").getValue();
var Zamanlama = Xrm.Page.data.entity.attributes.get("new_zamanlama").getValue();
var Email = Xrm.Page.data.entity.attributes.get("new_mail").getValue();
if (Xrm.Page.data.entity.attributes.get("new_siparis").getValue() != null) {
var SiparisId = Xrm.Page.data.entity.attributes.get("new_siparis").getValue()[0].id;
var SiparisName = Xrm.Page.data.entity.attributes.get("new_siparis").getValue()[0].name;
}
var Adet = Xrm.Page.data.entity.attributes.get("new_adet").getValue();
var Durumu = Xrm.Page.data.entity.attributes.get("incidentstagecode").getValue();
var telefon = Xrm.Page.data.entity.attributes.get("new_telefon").getValue();
var SrvOzt = Xrm.Page.data.entity.attributes.get("title").getValue();
if (Xrm.Page.data.entity.attributes.get("new_mhyetkilisi").getValue() != null) {
var MHyetkilisiId = Xrm.Page.data.entity.attributes.get("new_mhyetkilisi").getValue()[0].id;
var MHyetkilisiName = Xrm.Page.data.entity.attributes.get("new_mhyetkilisi").getValue()[0].name;
}
var islemBelgNo = Xrm.Page.data.entity.attributes.get("new_islembelgeno").getValue();
var Garanti = Xrm.Page.data.entity.attributes.get("new_garanti").getValue();
var SerTalTuru = Xrm.Page.data.entity.attributes.get("casetypecode").getValue();
var Sevkiyat = Xrm.Page.data.entity.attributes.get("new_sevkiyatsekli").getValue();
var SerAdrs = Xrm.Page.data.entity.attributes.get("new_servisadresi").getValue();
var Oncelik = Xrm.Page.data.entity.attributes.get("prioritycode").getValue();
var SemtIlce = Xrm.Page.data.entity.attributes.get("new_semt_ilce").getValue();
if (Xrm.Page.data.entity.attributes.get("new_sehir").getValue() != null) {
var SehirId = Xrm.Page.data.entity.attributes.get("new_sehir").getValue()[0].id;
var SehirName = Xrm.Page.data.entity.attributes.get("new_sehir").getValue()[0].name;
}
var TespitSnc = Xrm.Page.data.entity.attributes.get("new_tespitsonucu").getValue();
var HataKay = Xrm.Page.data.entity.attributes.get("new_mhhatakaynagi").getValue();
var HataAdi = Xrm.Page.data.entity.attributes.get("new_hatasebebi").getValue();
var HataliBrm = Xrm.Page.data.entity.attributes.get("new_hatalibirim").getValue();
var YplnHata = Xrm.Page.data.entity.attributes.get("new_hataadi").getValue();
var MHTop = Xrm.Page.data.entity.attributes.get("new_mhtoplanti").getValue();
var KapanisNdn = Xrm.Page.data.entity.attributes.get("new_kapansnedeni").getValue();
if (Xrm.Page.data.entity.attributes.get("new_satissor").getValue() != null) {
var SatSorId = Xrm.Page.data.entity.attributes.get("new_satissor").getValue()[0].id;
var SatSorName = Xrm.Page.data.entity.attributes.get("new_satissor").getValue()[0].name;
}
var Uygunsuzluk = Xrm.Page.data.entity.attributes.get("new_uygunsuzlukmaliyeti").getValue();
if (Xrm.Page.data.entity.attributes.get("new_departman").getValue() != null) {
var DepartmanId = Xrm.Page.data.entity.attributes.get("new_departman").getValue()[0].id;
var DepartmanName = Xrm.Page.data.entity.attributes.get("new_departman").getValue()[0].name;
}
var SerTalKay = Xrm.Page.data.entity.attributes.get("caseorigincode").getValue();
var SatFyt = Xrm.Page.data.entity.attributes.get("new_satisfiyati2").getValue();
if (Xrm.Page.data.entity.attributes.get("new_anketiyapan").getValue() != null) {
var AnketiYapanId = Xrm.Page.data.entity.attributes.get("new_anketiyapan").getValue()[0].id;
var AnketiYapanName = Xrm.Page.data.entity.attributes.get("new_anketiyapan").getValue()[0].name;
}
var AnketSonucu = Xrm.Page.data.entity.attributes.get("customersatisfactioncode").getValue();
var MusteriGors = Xrm.Page.data.entity.attributes.get("new_musterigorusleri").getValue();
var tekraronl = Xrm.Page.data.entity.attributes.get("new_tekraronleme").getValue();
var Aciklama = Xrm.Page.data.entity.attributes.get("description").getValue();
var parameters = {};
if (CustomerId != null && CustomerName != null) {
parameters["customerid"] = CustomerId;
parameters["customeridname"] = CustomerName;
parameters["customeridtype"] = CustomerType;
}
if (MatNumbId != null && MatNumbName != null) {
parameters["new_malzeme"] = MatNumbId;
parameters["new_malzemename"] = MatNumbName;
}
if (SatBurId != null && SatBurName != null) {
parameters["new_satisburosu"] = SatBurId;
parameters["new_satisburosuname"] = SatBurName;
}
if (Talep != null) {
parameters["new_talep"] = Talep;
}
if (ilgiliKisi != null) {
parameters["new_ilgilikisi"] = ilgiliKisi;
}
if (UrunAdi != null) {
parameters["new_urunadi"] = UrunAdi;
}
if (Zamanlama != null) {
parameters["new_zamanlama"] = Zamanlama;
}
if (Email != null) {
parameters["new_mail"] = Email;
}
if (SiparisId != null && SiparisName != null) {
parameters["new_siparis"] = SiparisId;
parameters["new_siparisname"] = SiparisName;
}
if (Adet != null) {
parameters["new_adet"] = Adet;
}
if (Durumu != null) {
parameters["incidentstagecode"] = Durumu;
}
if (telefon != null) {
parameters["new_telefon"] = telefon;
}
if (SrvOzt != null) {
parameters["title"] = SrvOzt;
}
if (MHyetkilisiId != null && MHyetkilisiName != null) {
parameters["new_mhyetkilisi"] = MHyetkilisiId;
parameters["new_mhyetkilisiname"] = MHyetkilisiName;
}
if (islemBelgNo != null) {
parameters["new_islembelgeno"] = islemBelgNo;
}
if (Garanti != null) {
parameters["new_garanti"] = Garanti;
}
if (SerTalTuru != null) {
parameters["casetypecode"] = SerTalTuru;
}
if (Sevkiyat != null) {
parameters["new_sevkiyatsekli"] = Sevkiyat;
}
if (SerAdrs != null) {
parameters["new_servisadresi"] = SerAdrs;
}
if (Oncelik != null) {
parameters["prioritycode"] = Oncelik;
}
if (SemtIlce != null) {
parameters["new_semt_ilce"] = SemtIlce;
}
if (TespitSnc != null) {
parameters["new_tespitsonucu"] = TespitSnc;
}
if (HataKay != null) {
parameters["new_mhhatakaynagi"] = HataKay;
}
if (HataAdi != null) {
parameters["new_hatasebebi"] = HataAdi;
}
if (HataliBrm != null) {
parameters["new_hatalibirim"] = HataliBrm;
}
if (YplnHata != null) {
parameters["new_hataadi"] = YplnHata;
}
if (MHTop != null) {
parameters["new_mhtoplanti"] = MHTop;
}
if (KapanisNdn != null) {
parameters["new_kapansnedeni"] = KapanisNdn;
}
/*if (SatSorId != null && SatSorName != null) {
parameters["new_satissor"] = SatSorId;
parameters["new_satissorname"] = SatSorName;
}*/
if (Uygunsuzluk != null) {
parameters["new_uygunsuzlukmaliyeti"] = Uygunsuzluk;
}
if (DepartmanId != null && DepartmanName != null) {
parameters["new_departman"] = DepartmanId;
parameters["new_departmanname"] = DepartmanName;
}
Xrm.Utility.openEntityForm("incident", null, parameters);
}
I am having two pages. The next button in first pages brings the second page. The focus in second page is always moving down. So I need to use scroll bar to bring the bring the cursor top. I want to bring the focus to the top. my focus-handler.js is as follows:
var lastFocusedControlId = "";
function focusHandler(e) {
document.activeElement = e.originalTarget;
}
function appInit() {
if (typeof (window.addEventListener) !== "undefined") {
window.addEventListener("focus", focusHandler, true);
}
setFirstControl()
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}
function pageLoadingHandler(sender, args) {
lastFocusedControlId = typeof (document.activeElement) === "undefined"
? "" : document.activeElement.id;
}
function focusControl(targetControl) {
if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
var focusTarget = targetControl;
if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
oldContentEditableSetting = focusTarget.contentEditable;
focusTarget.contentEditable = false;
}
else {
focusTarget = null;
}
try {
targetControl.focus();
if (focusTarget) {
focusTarget.contentEditable = oldContentEditableSetting;
}
}
catch (err) { }
}
else {
targetControl.focus();
}
}
function pageLoadedHandler(sender, args) {
if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {
focusControl(newFocused);
}
}
}
function setFirstControl() {
var bFound = false;
// for each form
for (f = 0; f < document.forms.length; f++) {
// for each element in each form
for (i = 0; i < document.forms[f].length; i++) {
// if it's not a hidden element
if (document.forms[f][i].type != "hidden") {
// and it's not disabled
if (document.forms[f][i].disabled != true) {
try {
// set the focus to it
document.forms[f][i].focus();
var bFound = true;
}
catch (er) {
}
}
}
// if found in this element, stop looking
if (bFound == true)
break;
}
// if found in this form, stop looking
if (bFound == true)
break;
}
}
Sys.Application.add_init(appInit);
How can I check if cookies are being blocked using JavaScript?
Write a cookie and then read it...
for example:
function readCookie(cname) {
a = trim(document.cookie);
res = '';
while(a != '') {
cookiename = a.substring(0,a.search('='));
cookiewert = a.substring(a.search('=')+1,a.search(';'));
if(cookiewert == '' || a.search(';')==-1) {
cookiewert = a.substring(a.search('=')+1,a.length);
} else {
cookiewert = a.substring(a.search('=')+1,a.search(';'));
}
if(cname == trim(cookiename)){
res = cookiewert;
return (res);
}
i = a.search(';')+1;
if(i == 0){i = a.length}
a = a.substring(i,a.length);
}
return(res)
}
function checkSessionCookie() {
//write cookie
document.cookie = 'sessioncookieallowed=true;';
var check = readCookie('sessioncookieallowed');
}
call function checkSessionCookie();