I am building an app using nativescript and i've noticed i have a ton of repeated code. The slideout drawer for example would have to be replicated on every page. How do I share the backend javascript so I can just load in a file or whatever instead of having to rewrite the code on every page?
For example I have a slideout drawer with view permissions and its called like this:
if (access[0]['page'] == 'Checks' && access[0]['enabled'] == true) {pageData.set("Checks", true);}
if (access[1]['page'] == 'Timeclock' && access[1]['enabled'] == true) {pageData.set("Timeclock", true);}
if (access[2]['page'] == 'Invoices' && access[2]['enabled'] == true) {pageData.set("Invoices", true);}
if (access[3]['page'] == 'EEInfo' && access[3]['enabled'] == true) {pageData.set("EEInfo", true);}
if (access[4]['page'] == 'CLInfo' && access[4]['enabled'] == true) {pageData.set("CLInfo", true);}
drawer = page.getViewById("drawer");
I don't want to have to repeat this on every page.
Related
I made a simple HTML website using HTML and JavaScript that suggests a song based on the attributes that the user selects. I have everything working, However, for example, in the image below, the second "else-if" statement and the last "else-if" statement end up returning the song title of the second "else-if" statement. In the last statement, it will return the song title that is in the second "else-if" statement. It looks like it is ignoring the band[0].checked part of the condition of the last "else-if" statement. I am unsure why it is just ignoring that part of the condition for the statement? My code for this part is below:
Everything works as it should, except for the last two "else-if" statements because of the issue I stated above.
var genre = document.getElementById("songGenre").value;
var bandList = document.getElementsByName("bandList");
var band = document.getElementsByName("band");
if (genre == "Rock" && bandList[0].checked && band[1].checked && band[2].checked) {
alert("Our song suggestion: Tip the Scales");
}
else if (genre == "Rock" && bandList[0].checked && band[1].checked && band[3].checked) {
alert("Our song suggestion: Savior");
}
else if (genre == "Rock" && bandList[0].checked && band[0].checked && band[2].checked) {
alert("Our song suggestion: Hero of War");
}
else if (genre == "Rock" && bandList[0].checked && band[0].checked && band[3].checked) {
alert("Our song suggestion: Swing Life Away");
}
else if (genre == "Rock" && bandList[0].checked && band[0].checked && band[1].checked && band[2].checked) {
alert("Our song suggestion: Entertainment");
}
else if (genre == "Rock" && bandList[0].checked && band[0].checked && band[1].checked && band[3].checked) {
alert("Our song suggestion: Elective Amnesia");
}
This is happening because when the condition:
(genre == "Rock" && bandList[0].checked && band[0].checked && band[1].checked && band[3].checked)
is true, then
(genre == "Rock" && bandList[0].checked && band[1].checked && band[3].checked)
is also true.
However, since you are using elseif, it will stop at the second condition and not continue checking down the line.
In order to fix this, you need to change the order of you elseif blocks, and put the most specific one in the top, and the more generic one in the bottom. In other words, put the longer complex if's on top, and simple short ones in the bottom.
Once you fix the order from specific --> simple, you will see your application behaving as expected.
I am trying to write this condition in javascript:
if(window.location.pathname != '/our-communities.php' && (window.location.pathname == '/upcoming-communities.php' && window.location.search != '')){
What I am trying to say is if the page is not our-communities.php and if the page is not upcoming-communities.php and window.location.search is not blank then run the code.
So this condition should run on every page except for our-communities.php but can run on upcoming-communities.php only if window.location.search is blank.
if (window.location.pathname != "/our-communities.php" || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}
One thing to note, if the page is "/upcoming-communities.php" then the first condition will always be true meaning the second OR statement is irrelevant, if you want to enforce that then extra conditionals are needed.
if ((window.location.pathname != "/our-communities.php" && window.location.pathname != "/upcoming-communities.php") || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}
I dont want to do the if statement when on pageAge OR ON pageMore. When i go to pageAge it works it doesn't execute the script but when I go to pageMore it does. I'm not sure what operator to use in this situation. When I put pageMore before the || it works on that page but not on the othe one.
if ( top.location.pathname != pageAge || pageMore) {
//if not verified go to connect
$("body").css("display", "none");
if (age === null && top.location.pathname != pageConnect) {
window.location.href = pageConnect;
}
//if to young go to age page while cookie is found (1day)
if (age == toYoung) {
window.location.href = pageAge;
}
//if already verified go to like page.
if (age == legal && top.location.pathname === pageConnect) {
window.location.href = pageLike;
}
}
It should be:
if ( top.location.pathname != pageAge && top.location.pathname != pageMore)
Forgive me if this is simple. I'm new to javascript.
I'm trying to make certain divs appear or hide based on the users answer to questions. I've created a function for each question that gets the results of that question based on their value. But I can't get the && additional condition to work. I need the div to appear ONLY if both conditions are true. It doesn't even seem to recognize anything from && and beyond. Q1 also sets some of the text in the div based on the answer. That seems to be working fine.
// Question 1
function analyzeQ1(answerQ1) {
if (answerQ1 == "TMC" || answerQ1 == "CMH" || answerQ1 == "SLH" || answerQ1 == "KU" || answerQ1 == "UMKC") {
document.getElementById('A1').innerHTML = " • Contact Research Administration at "+ answerQ1; + hideStuff('Q1a') + showStuff('A1')
} else if
(answerQ1 == "Other") {
showStuff('Q1a')
}
}
//Question 3
function analyzeQ3(answerQ3) {
if (answerQ3 == "no" && answerQ1 == "TMC") {
showStuff('A3') + hideStuff('Q3a')
} else if
(answerQ3 == "yes") {
showStuff('Q3a')
}
In the first code snippet you have a stray semicolon:
document.getElementById('A1').innerHTML = " • Contact Research Administration at "+ answerQ1;
hideStuff('Q1a');
showStuff('A1');
In the second code snippet you refer to answerQ1 but never pass it in to the function so you need:
//Question 3
function analyzeQ3(answerQ3, answerQ1) {
if (answerQ3 === "no" && answerQ1 === "TMC") {
showStuff('A3') + hideStuff('Q3a')
} else if
(answerQ3 === "yes") {
showStuff('Q3a')
}
Note that I used === vs ==. == is evil and you should forget that it exists in Javascript.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comparison_Operators
I'm trying to transform a blog on blogger into a website. In order to have a static home page I am using the Javascript code below to see if the user is on the home page if they are then it will hide the post section and display a home page "gadget". Is anything supposed to match anything?
document.onload = hidepage();
function hidepage () {
if (window.location == "http://website.blogspot.com/" || window.location == "http://website.blogspot.com/?zx=" + ANYTHING) {
//Checks to see if user is on the home page
$(".hentry").hide(); //Hide posts
$(".hfeed").hide(); //Hide posts
}
else {
$("#HTML2").hide(); //hide gadget
}
$(".post-title").hide(); //Hide post titles
}
Based on what you're saying I think you want to change the if condition to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("http://website.blogspot.com/?zx=") > -1)
You could also shorten this to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("/?zx=") > -1)
Note that I've changed your == to === as the latter is a literal comparison.
Just use String.indexOf in the second half of the if expression.
var url = window.location.href;
if (url === "http://website.blogspot.com/" || url.indexOf("http://website.blogspot.com/?zx=") === 0) {
// do stuff
}