I have a web page that checks for a cookie on page load and if the cookie does not exist the user is redirected to a certain log in page based on conditions. I am trying to add another condition to the cookie check but it is creating an infinite loop. Is there a way to allow another condition?
function checkCookie() {
var current_url = window.location.pathname;
var logInCookie = _siteNS.Utils.readCookie('x-access');
if (!logInCookie) {
if (
window.location.toString().includes('-fr') &&
current_url != '/landing-fr.html'
) {
window.location.replace('/landing-fr.html');
} else if (
window.location.href.indexOf('-fr') === -1 &&
current_url != '/landing.html'
) {
window.location.replace('/landing.html');
} else if (
window.location.href.indexOf('-fr') === -1 && *=== This is creating infinite loop===*
current_url === '/Important_Safety_Information.html'
) {
window.location.replace('/Important_Safety_Information.html');
}
}
}
The problem is that you're redirecting the user back to the page they are already on. When the user loads that same page, it's going to invoke that same logic again and redirect the user again... to the same page.
From a comment on the question above:
What I want to do is if the user goes to '/Important_Safety_Information.html' stay there and do not redirect
If you don't want to redirect the user then... don't redirect the user. Just remove that last else if block entirely since you don't want to perform that operation. Or, at worst, if you need to include this condition in an ongoing list of conditions then simply keep the block empty:
else if (
window.location.href.indexOf('-fr') === -1 &&
current_url === '/Important_Safety_Information.html'
) { /* do nothing */ }
Related
Ok so i am really basic with jQuery and the code i've written is no doubt inefficient but it works to a point.
I have a form where a customer selects either "Business" or "Personal" and on submission a cookie is set... I want to trigger images to change if the user has selected "Business" however for the changes to take effect the page needs reloading. How do i trigger this on selection of the option?
$(document).ready(function(){
$("#thankyoumessage").css("display","none");
$(".storagetype").click(function(){
if ($('input[name=customertype]:checked').val() == "Personal" ) {
$("#thankyoumessage").slideDown("fast"); //Slide Down Effect
$.cookie('customerIs', 'personal_cust', { path: '/', domain: 'storebox.co.uk'});
if ("ga" in window) {
tracker = ga.getAll()[0];
if (tracker)
tracker.send("event", "customer_type", "Personal");
};
} else if ($('input[name=customertype]:checked').val() == "Business" ) {
$("#thankyoumessage").slideDown("fast"); //Slide Down Effect
$.cookie('customerIs', 'business_cust', { path: '/', domain: 'storebox.co.uk'});
if ("ga" in window) {
tracker = ga.getAll()[0];
if (tracker)
tracker.send("event", "customer_type", "Business");
};
} else {
$("#thankyoumessage").slideUp("fast"); //Slide Up Effect
}
});
var customerIs = $.cookie('customerIs');
if (customerIs == 'personal_cust' || customerIs == 'business_cust') {
$("#customerTypeForm").css("display","none");
};
var customerIs = $.cookie('customerIs');
if (customerIs == 'business_cust') {
$('li.unit:nth-of-type(1) .unit__img-wrap img').attr('src', 'https://www.storebox.co.uk/wp-content/uploads/2021/06/unit-small-trade.png');
$('li.unit:nth-of-type(2) .unit__img-wrap img').attr('src', 'https://www.storebox.co.uk/wp-content/uploads/2021/06/unit-medium-trade.png');
$('li.unit:nth-of-type(3) .unit__img-wrap img').attr('src', 'https://www.storebox.co.uk/wp-content/uploads/2021/06/unit-large-trade.png');
};
});
Thanks in advance
Answering what I have in comments so you can mark question as resolved ;)
You can copy paste the "src" changes that you have at the end of your
code inside " else if ($('input[name=customertype]:checked').val() ==
"Business" ) "
Keep both parts, and once you are on the page for the first time it
changes it with the first part and on all the following times it will
be changed based on the cookie.
Im working on a project for school where I need to make a small system that requires 2 default login "accounts" and only that.
The way I do this is with an html form which is linked to a script.
For some reason when I use window.open('right.html'); it will put me through to the next page on a different tab once you fill in the right details.
But when I use window.open('right.html', '_self'); it will clear the field and not go to the next page.
Here is my script:
function checkform() {
if (document.login.name.value == "Thea" && document.login.pass.value == "1234") {
window.open('right.html');
} else {
if (document.login.name.value == "Theo" && document.login.pass.value == "4321") {
window.open('right2.html', "_self");
} else {
return false;
}
}
}
I'm trying to hide a form only if it's on the root site, or on the specific hash https://www.example.com/#uno. My code below is causing the form is not show on all the subpages (/#dos, /#tres, etc). Can someone help me understand what is happening?
$(function(){
if (location.hash == "" || location.hash == "#uno"){
$("#form").hide();
} else {
$("#form").show();
};
}());
This is because you hide the form once, but you don't make it reappear until you reload the page. (You only check the hash once, when loading the entire page, not when the hash changes.
function showHideForm() {
if (location.hash == "" || location.hash == "#uno"){
$("#form").hide();
} else {
$("#form").show();
};
}
window.onhashchange = showHideForm;
I'm trying to display a warning message when a user types certain text into an input box. The problem is I only want to return false one time so the user can submit the form on the second click even if they don't change the text. I've tried adding a counter but jquery is not remembering the count on a second sumbit click. What is the best way to go about this?
if (email.val().indexOf("gmail") > -1))
{
$('input[name=email]').css('border-color','red');
$("#submit").after('<p>Error - Do you want to use a gmail account?</p>');
return false;
}
I would use a flag to determine if they have already tried to submit, and if they haven't, then you give them the warning and return false:
var triedSubmit = false;
$(/* Your jQuery Object */).click(function() {
if (email.val().indexOf("gmail") > -1))
{
if (!triedSubmit){
$('input[name=email]').css('border-color','red');
$("#submit").after('<p>Error - Do you want to use a gmail account?</p>');
triedSubmit = true;
return false;
}
}
}
Just set up some kind of flag
var flags = {}; // in some higher scope
// then later, in your verification function
if (email.val().indexOf("gmail") > -1 && !flags.warnedGmail) {
$('input[name=email]').css('border-color','red');
$("#submit").after('<p>Error - Do you want to use a gmail account?</p>');
flags.warnedGmail = true;
return false;
}
Why don't you put a class on your text box and remove it in the first failure? Thus when you look for it with jQuery a second time you won't be able to find it it and won't be able to apply the rule. I implmented it like so:
var submit = function()
{
var email = $('.emailFirstTry')
if (email.length > 0 && email.val().indexOf("gmail") > -1)
{
$('input[name=email]').css('border-color','red');
$("#submit").text('Error - Do you want to use a gmail account');
$('.emailFirstTry').removeClass('emailFirstTry');
return false;
}
$('input[name=email]').css('border-color','none');
$("#submit").text('Success!');
return true;
};
You can see it in action on this fiddle right here: http://jsfiddle.net/ozrevulsion/39wjbwcr/
Hope the helps :)
I'm currently using this code:
var domains = ["domain.net", "domain.com", "domain.info"];
if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('redirected=1') == -1)
{
window.top.location="http://domain.net";
document.cookie = "redirected=1";
alert("Redirecting to our main website");
}
else if (domains.indexOf(document.location.hostname) == 1)
{
document.cookie = "redirected=1";
}
alert("Thanks for joining our main website");
if i move the second alert over "}" it will not give any alert, and when i leave it as it is i will get sequential alerts starting with 1st alert and then second alert, then when page redirects i will get second alert again.
is there a way to fix this ?
Thanks in advance