Button just works on Internet Explorer - javascript

I am trying to solve a problem inside my dad's enterprise system, the system has a button, that works just on Internet Explorer, it doesn't work in other browsers, so it is not possible to work in this system using iPad or another Operational System... They bought this system from an enterprise called Linx, and i guess this system's code is too dangerous, dealing with queries on the client-side (javascript).
If you know why this just works on Internet Explorer i would be so greatful!
Thanks in advice...
Observation: i just copied the button's html and the button's javascript function, and paste here.
<script>
function Query_onclick() {
xwhere = "";
xflag = 1;
if (xmodelos != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Modelagem = '" + xmodelos + "'";
}
if (xmateriais != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Material = '" + xmateriais + "'";
}
if (xgrupos != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Grupo_produto = '" + xgrupos + "'";
}
if (xsubgrupos != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Subgrupo_produto = '" + xsubgrupos + "'";
}
if (xcategorias != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " p.cod_categoria in (select cod_categoria from produtos_categoria where CATEGORIA_PRODUTO ='" + xcategorias + "')";
}
if (xsubcategorias != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " p.cod_subcategoria in (select cod_subcategoria from produtos_subcategoria where SUBCATEGORIA_PRODUTO ='" + xsubcategorias + "')";
}
if (xfabricante != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Fabricante = '" + xfabricante + "'";
}
if (xlinhas != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Linha = '" + xlinhas + "'";
}
if (xcomposicao != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " composicao = '" + xcomposicao + "'";
}
if (xcolecoes != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Colecao = '" + xcolecoes + "'";
}
if (xgriffes != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Griffe = '" + xgriffes + "'";
}
if (xtipos != ""){
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " Tipo_produto = '" + xtipos + "'";
}
if (produto.value != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " produto like '" + Urlencode("%" + produto.value + "%") + "'";
}
if (descricao.value != "") {
if (xwhere != "") {
xwhere = xwhere + " and ";
}
xwhere = xwhere + " desc_produto like '" + urlencode(descricao.value) + "'";
}
if (xwhere != ""){
if ( Foto.checked == 1) {
xfoto = "&xfoto=" + Foto.checked;
}
else {
xfoto = "&xfoto=false";
}
parent.frames.Principal.location = "../pages/cat_prods.asp?xwhere=" + xwhere + xfoto;
}
else{
if ( Foto.checked == 1) {
xfoto = "?xfoto=" + Foto.checked;
}
else {
xfoto = "?xfoto=false";
}
parent.frames.Principal.location = "../pages/cat_prods.asp"+ xfoto;
}
}
</script>
<input type="Image" src="image.png" name="Query" value=" Pesquisar " onclick="Query_onclick()">

You could try changing from parent.frames.Principal.location to parent.frames.Principal.location.href.

Related

JavaScript TypeError only in IE 11

When loading a js e-form in IE getting an error 'loadForm: if GroupDocList>0: TypeError: Unable to get property 'length; of undefined or null reference
This only occurs in IE not Chrome or Edge.
This form was coded by someone else and I don't have much experience with JS
Update * Through debugging I was able to find that it is the CompList object that is undefined and causing this error. Wondering why this is being undefined at this point within IE but working within Chrome and Edge or at least not throwing the error.
for (var i = 0; i < CompList.length; i++) {
function CreatePopulateDataSet() {
//*************************************************************************
//Company/Division/SubCat/Plan dataset populate - BEGIN********************
//*************************************************************************
var g_CompanyList = {};
var g_DivisionList = {};
var g_SubCatList = {};
try {
if ($("#selCompanyList").find("option").length > 0) {
try {
$("#selCompanyList option").each(function () {
var Comp = $(this).text().split("|")[0];
var Div = $(this).text().split("|")[1];
var SubCat = $(this).text().split("|")[2];
var Plant = $(this).text().split("|")[3];
var CompList = g_CompanyList[Comp];
var DivList = g_DivisionList[Comp + Div];
var SubList = g_SubCatList[Comp + Div + SubCat];
try {
if (hasCompanyAccess(Comp)) {
if (!CompList) { CompList = new Array(); }
if ($.inArray(Div, CompList) == -1) {
CompList.push(Div);
g_CompanyList[Comp] = CompList;
}
if (!DivList) { DivList = new Array(); }
if (SubCat != null) {
if ($.inArray(SubCat, DivList) == -1) {
DivList.push(SubCat);
g_DivisionList[Comp + Div] = DivList;
}
}
if (!SubList) { SubList = new Array(); }
if (Plant != null) {
if ($.inArray(Plant, SubList) == -1) {
SubList.push(Plant);
g_SubCatList[Comp + Div + SubCat] = SubList;
}
}
}
}
catch (err) {
alert("Unable build global lists used to populate drop downs. " + err);
}
});
}
catch (err) {
alert(err);
}
$("#selPARCompany").html("<option value=''>Please Select...</option>");
for (var Comp in g_CompanyList) {
$("#selPARCompany").append("<option value='" + Comp + "'>" + Comp + "</option>");
}
$("#selPARCompany").addClass("fullwidth");
//Cascade Reload - BEGIN
//Re-loads/populates the cascading data set if the form has been submitted.
if ($("#OBPropertyItemNum").val()) {
//re-sets the Company drop down box on load of a submitted form.
var tempComp = $("#txtPARCompany").val();
$("#selPARCompany option").each(function () {
if ($(this).text().toUpperCase() == tempComp.toUpperCase()) {
$(this).attr("selected", "selected");
}
});
//Calls the CompanyTabOverride method to set access to the corrct tabs based on the saved value.
CompanyTabOverride(tempComp);
var CompList = g_CompanyList[$("#selPARCompany option:selected").text().toUpperCase()];
var tempDiv = $("#txtPARDivision").val();
//re-load and populate the Division drop down box
for (var i = 0; i < CompList.length; i++) {
if (i == 0)
$("#selPARDivision").html("<option value=''>Please Select...</option>");
if (CompList[i].toUpperCase() == tempDiv.toUpperCase()) {
$("#selPARDivision").append("<option selected=selected label='" + CompList[i] + "' value='" + CompList[i] + "'>" + tempComp + CompList[i] + "</option>");
} else {
$("#selPARDivision").append("<option label='" + CompList[i] + "' value='" + CompList[i] + "'>" + tempComp + CompList[i] + "</option>");
}
}
if ($("#selPARDivision option").length > 1) {
$("#selPARDivision").closest("td").show();
$("#selPARDivision").addClass("required");
} else {
$("#selPARDivision").closest("td").hide();
$("#selPARDivision").removeClass("required");
$("#selPARDivision").removeClass("requiredInput");
}
DivisionTabOverride(tempComp, tempDiv);
//re-load and populate the Sub Category drop down box
var tempSubCat = $("#txtPARSubCat").val();
var DivList = g_DivisionList[$("#selPARDivision option:selected").text().toUpperCase()];
var Div = $("#selPARDivision option:selected").html();
if (tempSubCat != "") {
for (var i = 0; i < DivList.length; i++) {
if (i == 0)
$("#selPARSubCat").html("<option value=''>Please Select...</option>");
if (DivList[i].toUpperCase() == tempSubCat.toUpperCase()) {
$("#selPARSubCat").append("<option selected=selected label='" + DivList[i] + "' value='" + DivList[i] + "'>" + Div + DivList[i] + "</option>");
} else {
$("#selPARSubCat").append("<option label='" + DivList[i] + "' value='" + DivList[i] + "'>" + Div + DivList[i] + "</option>");
}
}
if ($("#selPARSubCat option").length > 1) {
$("#selPARSubCat").closest("td").show();
$("#selPARSubCat").addClass("required");
} else {
$("#selPARSubCat").closest("td").hide();
$("#selPARSubCat").removeClass("required");
$("#selPARSubCat").removeClass("requiredInput");
}
}
//re-load and populate the Plant drop down box.
var tempPlant = $("#txtPARPlant").val();
var SubList = g_SubCatList[$("#selPARSubCat option:selected").text().toUpperCase()];
var SubCat = $("#selPARSubCat option:selected").html();
if (tempPlant != "") {
for (var i = 0; i < SubList.length; i++) {
if (i == 0)
$("#selPARPlant").html("<option value=''>Please Select...</option>");
if (SubList[i].toUpperCase() == tempPlant.toUpperCase()) {
$("#selPARPlant").append("<option selected=selected label='" + SubList[i] + "' value='" + SubList[i] + "'>" + SubCat + SubList[i] + "</option>");
} else {
$("#selPARPlant").append("<option label='" + SubList[i] + "' value='" + SubList[i] + "'>" + SubCat + SubList[i] + "</option>");
}
}
if ($("#selPARPlant option").length > 1) {
$("#selPARPlant").closest("td").show();
$("#selPARPlant").addClass("required");
} else {
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
}
}
}
//Cascade Reload - END
try {
$("#selPARCompany").bind("change", function () {
var Comp = $(this).val();
$("#selPARDivision").html("<option value=''>Please Select...</option>");
$("#txtPARDivision").val("");
$("#selPARSubCat").html("<option value=''>Please Select...</option>");
$("#txtPARSubCat").val("");
$("#selPARPlant").html("<option value=''>Please Select...</option>");
$("#txtPARPlant").val("");
if ($(this)[0].selectedIndex > 0) {
var CompList = g_CompanyList[Comp];
for (var i = 0; i < CompList.length; i++) {
//$("#selPARDivision").append("<option value='" + Comp + CompList[i] + "'>" + CompList[i] + "</option>");
$("#selPARDivision").append("<option label='" + CompList[i] + "' value='" + CompList[i] + "'>" + Comp + CompList[i] + "</option>");
}
}
$("#selPARDivision").addClass("fullwidth");
$("#txtPARCompany").val($("#selPARCompany").val());
if ($("#selPARDivision option").length > 1) {
$("#selPARDivision").closest("td").show();
$("#selPARDivision").addClass("required");
$("#selPARSubCat").closest("td").hide();
$("#selPARSubCat").removeClass("required");
$("#selPARSubCat").removeClass("requiredInput");
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
} else {
$("#selPARDivision").closest("td").hide();
$("#selPARDivision").removeClass("required");
$("#selPARDivision").removeClass("requiredInput");
$("#selPARSubCat").closest("td").hide();
$("#selPARSubCat").removeClass("required");
$("#selPARSubCat").removeClass("requiredInput");
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
}
CompanyTabOverride(Comp);
IsEngineeringLevelRequired(Comp);
IsUniqPlanIDRequired(Comp, $('#selPARExpenditureinBusinessPlan').find(":selected").text());
//$('#selPARExpenditureinBusinessPlan').change();
});
$("#selPARDivision").bind("change", function () {
var Div = $("#" + $(this).attr("id") + " option:selected").html();
$("#selPARSubCat").html("<option value=''>Please Select...</option>");
$("#txtPARSubCat").val("");
$("#selPARPlant").html("<option value=''>Please Select...</option>");
$("#txtPARPlant").val("");
if ($(this)[0].selectedIndex > 0) {
var DivList = g_DivisionList[Div];
if (DivList) {
for (var i = 0; i < DivList.length; i++) {
//$("#selPARSubCat").append("<option value='" + Div + DivList[i] + "'>" + DivList[i] + "</option>");
$("#selPARSubCat").append("<option label='" + DivList[i] + "' value='" + DivList[i] + "'>" + Div + DivList[i] + "</option>");
}
}
}
$("#selPARSubCat").addClass("fullwidth");
$("#txtPARDivision").val($("#selPARDivision").val());
if ($("#selPARSubCat option").length > 1) {
$("#selPARSubCat").closest("td").show();
$("#selPARSubCat").addClass("required");
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
} else {
$("#selPARSubCat").closest("td").hide();
$("#selPARSubCat").removeClass("required");
$("#selPARSubCat").removeClass("requiredInput");
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
}
DivisionTabOverride($("#selPARCompany").val(), $(this).val());
});
$("#selPARSubCat").bind("change", function () {
//var SubCat = $(this).val();
var SubCat = $("#" + $(this).attr("id") + " option:selected").html();
$("#selPARPlant").html("<option value=''>Please Select...</option>");
$("#txtPARPlant").val("");
if ($(this)[0].selectedIndex > 0) {
var SubList = g_SubCatList[SubCat];
if (SubList) {
for (var i = 0; i < SubList.length; i++) {
//$("#selPARPlant").append("<option value='" + SubCat + SubList[i] + "'>" + SubList[i] + "</option>");
$("#selPARPlant").append("<option label='" + SubList[i] + "' value='" + SubList[i] + "'>" + SubCat + SubList[i] + "</option>");
}
}
}
$("#selPARPlant").addClass("fullwidth");
$("#txtPARSubCat").val($("#selPARSubCat").val());
if ($("#selPARPlant option").length > 1) {
$("#selPARPlant").closest("td").show();
$("#selPARPlant").addClass("required");
} else {
$("#selPARPlant").closest("td").hide();
$("#selPARPlant").removeClass("required");
$("#selPARPlant").removeClass("requiredInput");
}
});
$("#selPARPlant").bind("change", function () {
$("#txtPARPlant").val($("#selPARPlant").val());
});
}
catch (err) {
alert("loadForm: if GroupDocList>0: bind Group Change:" + err);
}
}
}
catch (err) {
alert("loadForm: if GroupDocList>0: " + err);
}

how can the if (){}else{} statement execute both outcomes

I want to create a basic search engine that searches students already existing in an array of objects by first name and last name and if it finds a student named that way enlist it on the page, and if it doesn't write on the page it doesn't exist. but when I have 2 people with the same first name, it gives me both outcome of the if statement. can someone help, please
searchButton.addEventListener("click", function () {
userSearch = searchInput.value;
for (i = 0; i < allStudents.length; i++) {
student = allStudents[i];
if(userSearch.toLowerCase() === student.firstName.toLowerCase() ||
userSearch.toLowerCase() === student.lastName.toLowerCase() ||
userSearch.toLowerCase() === student.firstName.toLowerCase() + " " + student.lastName.toLowerCase() ||
userSearch.toLowerCase() === student.lastName.toLowerCase() + " " + student.firstName.toLowerCase()) {
outputDiv.innerHTML += "<h2> Student: " + student.firstName + " " + student.lastName + "</h2><br>" +
"Age: " + student.age + "<br>" +
"Eye Color: " + student.eyeColor + "<br>" +
"Hair Color: " + student.hairColor + "<br>" +
"Programming Skills: " + student.programmingSkills
searchInput.value = "";
} else {
searchInput.value = "";
outputDiv.innerHTML += "<h2>The student you searched for is not in out database</h2>"
}
}
});
You might want to change your logic as you have two tasks:
Find if the student exists
Display students if available else show message
You can use a variable (found) to keep track of whether any students were found in allStudents, that way you won't have to worry about the else condition executing more than once
let allStudents = [{
firstName: "George",
lastName: "A"
}, {
firstName: "George",
lastName: "B"
}]
searchButton.addEventListener("click", function() {
outputDiv.innerHTML = "";
userSearch = searchInput.value;
var found = false;
for (i = 0; i < allStudents.length; i++) {
student = allStudents[i];
if (userSearch.toLowerCase() === student.firstName.toLowerCase() ||
userSearch.toLowerCase() === student.lastName.toLowerCase() ||
userSearch.toLowerCase() === student.firstName.toLowerCase() + " " + student.lastName.toLowerCase() ||
userSearch.toLowerCase() === student.lastName.toLowerCase() + " " + student.firstName.toLowerCase()) {
outputDiv.innerHTML += "<h2> Student: " + student.firstName + " " + student.lastName + "</h2><br>" +
"Age: " + student.age + "<br>" +
"Eye Color: " + student.eyeColor + "<br>" +
"Hair Color: " + student.hairColor + "<br>" +
"Programming Skills: " + student.programmingSkills
found = true;
}
}
if (!found)
outputDiv.innerHTML = "<h2>The student you searched for is not in out database</h2>";
searchInput.value = "";
});
<input id="searchInput" value="George" />
<button id="searchButton">search</button>
<div id="outputDiv"></div>
You need to rearrange the check to see if it's found or not. You are outputting "that student was not found in our database" INSIDE the loop of students, so it's outputting that text even if another result (in the loop) WAS found. I have corrected that issue by moving the check (for if a student was found) outside of the loop and using a variable to track that. I also changed some things so that you aren't reformatting names toLowerCase() in every loop or having to locate a DOM element.
searchInput = document.getElementById("search_text"),
searchButton = document.getElementById("SearchButton"),
outputDiv = document.getElementById("outputdiv");
searchButton.addEventListener("click", function () {
var userFound = false;
userSearch = searchInput.value;
studentLen = allStudents.length;
outputDiv.innerHTML = "";
for (i = 0; i < studentLen; i++) {
student = allStudents[i];
userSearch = userSearch.toLowerCase();
var fn = student.firstName.toLowerCase(),
ln = student.lastName.toLowerCase();
if(userSearch === fn || userSearch === ln || userSearch === fn + " " + ln || userSearch === ln + " " + fn) {
outputDiv.innerHTML += "<h2> Student: " + student.firstName + " " + student.lastName + "</h2><br>" +
"Age: " + student.age + "<br>" +
"Eye Color: " + student.eyeColor + "<br>" +
"Hair Color: " + student.hairColor + "<br>" +
"Programming Skills: " + student.programmingSkills
searchInput.value = "";
userFound = true;
}
}
if(!userFound){
outputDiv.innerHTML += "<h2>The student you searched for is not in out database</h2>";
}
});
Working fiddle with HTML/JS both included:
https://jsfiddle.net/Ltkacgn4/

How to use switch case for validate multiple variable

I am using one javascript where i used if condition to validate more than two variable, Based on that i am setting the data.
Below is javascript code:
if (twitterPicLink != "" || twitterMediaLink != "" || twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet'>";
htmlDiv += "<div class='tweet-image'><img src='" + imgSrc + "' title='Tweet icon'></div>";
}
if (twitterPicLink != "" && twitterMediaLink != "" && twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
}
else if (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterMediaLink + "</p></div></div>";
}
else if (twitterPicLink == "" && twitterMediaLink == "" && twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + "</p></div></div>";
}
else if (twitterPicLink != "" && twitterMediaLink == "" && twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitteHeartServeLink + " " + twitterPicLink + "</p></div></div>";
}
else if (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitteHeartServeLink + " " + twitterMediaLink + "</p></div></div>";
}
else if (twitterPicLink != "" && twitterMediaLink != "" && twitteHeartServeLink == "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
}
else if (twitterPicLink != "" && twitterMediaLink == "" && twitteHeartServeLink == "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterPicLink + "</p></div></div>";
}
else if (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink == "") {
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterMediaLink + "</p></div></div>";
}
How can i change above code to switch case validation?
Think about your design. Even though this does not technically answer the question you could to the following with a factory-ish class
if (twitterPicLink != "" || twitterMediaLink != "" || twitteHeartServeLink != "") {
htmlDiv += "<div class='tweet'>";
htmlDiv += "<div class='tweet-image'><img src='" + imgSrc + "' title='Tweet icon'></div>";
}
htmlDiv += tweetBodyFactory.create(twitterPicLink, twitterMediaLink, twitterHeartSaveLink);
Within your factory you can use composition to hold factory implementations, that define the fields PicLinkIsDefined, MediaLinkIsDefined and HeartSaveLinkIsDefined. With
var matchingFactory = factories.find(f => (f.PicLinkIsDefined && twitterPicLink != "" || !f.PicLinkIsDefined && tweitterPicLink == "")
&& (f.MediaLinkIsDefined && twitterMediaLink != "" || !f.MediaLinkIsDefined && twitterMediaLink == "")
&& (f.HeartSaveLinkIsDefined && twitterHeartSaveLink != "" || !f.HeartSaveLinkIsDefined && twitterHeartSaveLink == ""));
if(matchingFactory != undefined)
{
return matchingFactory.create(twitterPicLink, twitterMediaLink, twitterHeartSaveLink);
}
// error handling here
you can select the matching factory and build the HTML. It'll be a bit more code (with all the factory implementations), but in the end it'll be much cleaner.
I know that it's a bit loose interpretation of the factory pattern, but still a good way to structure the requirements.
I hope this would help
switch (twitterPicLink) {
case "":
switch (twitterMediaLink) {
case "":
switch (twitteHeartServeLink) {
case "":
[break;]
default:
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + "</p></div></div>";
[break;]
}
[break;]
default:
switch (twitteHeartServeLink) {
case "":
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterMediaLink + "</p></div></div>";
[break;]
default:
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterMediaLink + "</p></div></div>";
[break;]
}
[break;]
}
[break;]
default:
switch (twitterMediaLink) {
case "":
switch (twitteHeartServeLink) {
case "":
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterPicLink + "</p></div></div>";
[break;]
default:
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitteHeartServeLink + " " + twitterPicLink + "</p></div></div>";
[break;]
}
[break;]
default:
switch (twitteHeartServeLink) {
case "":
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
[break;]
default:
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
[break;]
}
[break;]
}
[break;]
}
Try cover your code in switch case..
Please check it once..
switch (true) {
case (twitterPicLink != "" && twitterMediaLink != "" && twitteHeartServeLink != ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
break;
case (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink != ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + " <br/>" + twitterMediaLink + "</p></div></div>";
break;
case (twitterPicLink == "" && twitterMediaLink == "" && twitteHeartServeLink != ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitteHeartServeLink + "</p></div></div>";
break;
case (twitterPicLink != "" && twitterMediaLink == "" && twitteHeartServeLink != ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitteHeartServeLink + " " + twitterPicLink + "</p></div></div>";
break;
case (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink != ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitteHeartServeLink + " " + twitterMediaLink + "</p></div></div>";
break;
case (twitterPicLink != "" && twitterMediaLink != "" && twitteHeartServeLink == ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + "<br/>" + twitterPicLink + " " + twitterMediaLink + "</p></div></div>";
break;
case (twitterPicLink != "" && twitterMediaLink == "" && twitteHeartServeLink == ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterPicLink + "</p></div></div>";
break;
case (twitterPicLink == "" && twitterMediaLink != "" && twitteHeartServeLink == ""):
htmlDiv += "<div class='tweet-body'><p>" + twittedText + " " + twitterMediaLink + "</p></div></div>";
break;
htmlDiv += "<div class='tweet'>";
htmlDiv += "<div class='tweet-image'><img src='" + imgSrc + "' title='Tweet icon'></div>";
default:
}
you can study switch - JavaScript from MDN
Here's a code synxtax on how it is used.
switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed when the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}

javascript amendment if within an if

I've never used javascript before and just need a bit of help. I have this:
if (vars.devUrl != '')
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
But I need to amend it to say if the vars.smsmessage includes the string "http:" then suppress the vars.devUrl
I've come up with the below, would an if in an if work?
if (vars.devUrl != '')
{
if (vars.smsmessage includes("http:"))
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
}
delivery.smsParameters.smsContent = vars.smsmessage;
Also should add that I can't test this before I go live very easily at all, hence the request
Very close, but the else comes outside the if block, and you need to use indexOf in Javascript to check if one string contains another, like so:
if (vars.devUrl) {
if (vars.smsmessage.indexOf('http:') > -1) {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.optOut;
}
else {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.devUrl + '\n\n' + vars.optOut;
}
}
else {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
However, you can make this a little easier on the eye using a ternary conditional like so:
var url = vars.smsmessage.indexOf('http:') > -1 ? vars.devUrl + '\n\n' : '';
vars.smsmessage += '\n\n' + url + vars.optout;
delivery.smsParameters.smsContent = vars.smsmessage;
If I'm not mistaken, for this specific request your intial code don't need and extra if, simply an extra condition :
if (vars.devUrl != '' && vars.devUrl.indexOf('http:') < 0)
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
So; basically, you use vars.devUrl only if it's not empty AND if it doesn't contain "http:"

Fix incorrect nesting

function drawinventoryList() {
inventoryArray.sort();
var inventoryString = "";
for (x in inventoryArray) {
arrayValue = inventoryArray[x];
var counter = parseInt(x) + 1;
if (counter == 1) {
inventoryString = "<table width='100%' celpadding='0' cellspacing='0' style='border:1px solid #d00;'>"
}
if (arrayValue.substring(0, arrayValue.indexOf("#")) != "XXXXXXXX") {
inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("#")) + ", " + arrayValue.substring(arrayValue.indexOf("#") + 1) + " (remove)</td></tr>";
}
}
if (inventoryString == "") inventoryString = "None."
document.getElementById("selectedInventories").innerHTML = inventoryString;
}
... this function creates an invalid table. I need the 'counter' 'name' and 'remove link' in separate columns. How do I correctly create and close all tags?
Many thanks
It looks like you're just forgetting the closing tag for the table. Try this instead:
function drawinventoryList() {
inventoryArray.sort();
var inventoryString = "";
for (x in inventoryArray) {
arrayValue = inventoryArray[x];
var counter = parseInt(x) + 1;
if (counter == 1) {
inventoryString = "<table width='100%' celpadding='0' cellspacing='0' style='border:1px solid #d00;'>"
}
if (arrayValue.substring(0, arrayValue.indexOf("#")) != "XXXXXXXX") {
inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("#")) + ", " + arrayValue.substring(arrayValue.indexOf("#") + 1) + " (remove)</td></tr>";
}
}
if (inventoryString == "") {
inventoryString = "None.";
} else {
inventoryString = inventoryString + "</table>";
}
document.getElementById("selectedInventories").innerHTML = inventoryString;
}
The only change was from:
if (inventoryString == "") inventoryString = "None."
to:
if (inventoryString == "") {
inventoryString = "None.";
} else {
inventoryString = inventoryString + "</table>";
}
Update: In your comment, you stated that
I still need to separate counter, name and link in separated [sic] TD
That's probably just a matter of inserting some </td><td> sections, something like:
inventoryString = inventoryString +
"<tr>" +
"<td>" + counter + ".</td>" +
"<td>" + arrayValue.substring(0, arrayValue.indexOf("#")) + ", " +
arrayValue.substring(arrayValue.indexOf("#") + 1) + "</td>" +
"<td>(<a href=\"javascript:removeinventory('" + x +
"')\">remove</a>)</td>" +
"</tr>";
I am not entirely sure what you are trying to do but maybe this might be what you are looking for.
function drawinventoryList() {
inventoryArray.sort();
var inventoryString = "<table width='100%' celpadding='0' cellspacing='0' style='border:1px solid #d00;'>";
var count = 0;
for (x in inventoryArray) {
arrayValue = inventoryArray[x];
count++;
if (arrayValue.substring(0, arrayValue.indexOf("#")) != "XXXXXXXX") {
inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("#")) + ", " + arrayValue.substring(arrayValue.indexOf("#") + 1) + " (remove)</td></tr>";
}
}
if (count == 0) {
inventoryString = "None.";
} else {
inventoryString = inventoryString + "</table>";
}
document.getElementById("selectedInventories").innerHTML = inventoryString;
}

Categories