Show login screen If the user is not logged in | jQuery - javascript

I try to display Login Screen in the spesific URL ('ads/audience-insights') If the user is not logged in. but no luck.
Can you correct my code? what's wrong?
if (window.location.href.indexOf('ads/audience-insights') != -1) {
chrome.storage.local.get("cnt9", function(result) {
if (result.cnt9 == null || result.cnt9 == -1 || result.cnt9 >= 40) {
// show login screen
showLoginScreen();
}
});
}
This code == user Not logged in yet..
result.cnt9 == null || result.cnt9 == -1 || result.cnt9 >= 40

You don't have to use chrome.storage or the callback function.
if (typeof(Storage) !== "undefined") {
var a = localStorage.cnt9;
if (a == null || a == -1 || a >= 40)
alert("Not Logged In!");
}
You also do not have to check if the user is on a specific page as the code will not execute if they are not on said page in the first place.

Related

How do I correctly remove a class from the Place Order button upon error message?

It's a WooCommerce Stripe card payment.
I can correctly add the class on button click like this:
$('form.checkout').on('submit', function(event) {
event.preventDefault();
$('button#place_order').addClass('my_class');
But my code below is unable to remove it upon error message (after the button is clicked):
$('body').on('woocommerce_error', function(){
console.log('woocommerce_error triggered');
$('button#place_order').removeClass('my_class');
});
The woocommerce_error class is added to the page HTML (this is on the cart) when an error occurs, but not to the button HTML.
using checkout_error doesn't work either - the class is not removed
There is no checkout_error class added to the button or to the page HTML when the error message occurs, but I can find it in the JS when I click on the error message in the console.
But it seems more like a notice than an error message in the console, it looks like this:
I am not sure wether this is a post validation or pre-validation use case.
Because checkout_error can occur only after the validation, is that right?
This is the JS code I get in the console when I click on the error notice:
if (t = s ? (a("body").removeClass("woocommerce-stripe-prb-clicked"),
a("div.woocommerce-notices-wrapper").first()) : i.length ?
(t = i.filter(":checked")).closest(".woocommerce-SavedPaymentMethods-new").length ?
a("#wc-stripe-cc-form .stripe-source-errors") : t.closest("li").find(".stripe-source-errors") : n.find(".stripe-source-errors"),
m.isSepaChosen() && "invalid_owner_name" === r.error.code && wc_stripe_params.hasOwnProperty(r.error.code)) {
n = a('<div><ul class="woocommerce-error"><li /></ul></div>');
return n.find("li").text(wc_stripe_params[r.error.code]),
void m.submitError(n.html())
}
"email_invalid" === r.error.code ? o = wc_stripe_params.email_invalid : "invalid_request_error" !== r.error.type && "api_connection_error" !== r.error.type && "api_error" !== r.error.type && "authentication_error" !== r.error.type && "rate_limit_error" !== r.error.type || (o = wc_stripe_params.invalid_request_error),
"card_error" === r.error.type && wc_stripe_params.hasOwnProperty(r.error.code) && (o = wc_stripe_params[r.error.code]),
"validation_error" === r.error.type && wc_stripe_params.hasOwnProperty(r.error.code) && (o = wc_stripe_params[r.error.code]),
m.reset(),
a(".woocommerce-NoticeGroup-checkout").remove(),
console.log(r.error.message),
a(t).html('<ul class="woocommerce_error woocommerce-error wc-stripe-error"><li /></ul>'),
a(t).find("li").text(o),
a(".wc-stripe-error").length && a("html, body").animate({
scrollTop: a(".wc-stripe-error").offset().top - 200
}, 200),
m.unblock(),
a.unblockUI()
},
submitError: function(e) {
a(".woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message").remove(),
m.form.prepend('<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + e + "</div>"),
m.form.removeClass("processing").unblock(),
m.form.find(".input-text, select, input:checkbox").trigger("blur");
e = "";
a("#add_payment_method").length && (e = a("#add_payment_method")),
a("#order_review").length && (e = a("#order_review")),
(e = a("form.checkout").length ? a("form.checkout") : e).length && a("html, body").animate({
scrollTop: e.offset().top - 100
}, 500),
a(document.body).trigger("checkout_error"),
m.unblock()
And the
console.log(r.error.message),
row is highlighted.
I have also tried to bind to the error message text itself like this:
jQuery('.woocommerce-error li').each(function(){
var error_text = $(this).text();
if (error_text == 'The card number is incomplete.'){
jQuery('button#place_order').removeClass('my_class');
but it still won't work, the class will not be removed.
I have also tried to use toggleClass instead, still won't work.

Unhide an external button only when multiple indipendent input fields are filled elsewhere

I need to show a specific button only when some fields in another form are filled.
Working on wordpress and woocommerce.
These fields come from a plugin that gives me custom fields for woocommerce.
The button is from another plugin about a configurator, normally that button is accessible as soon as you enter the product page, it let's you customize stuff.
Even though these fileds are required I need to make sure customers fill those first, that's why I thought about hiding the customizer button untill all previous fields are filled.
After so many tries i finally found a solution but unfortunately not so scalable
jQuery(document).ready(function($){
var checkExist = setInterval(function() {
if (
($('.collo input[required]').val() != '') &&
($('.spalle input[required]').val() != '') &&
($('.manica input[required]').val() != '') &&
($('.torace input[required]').val() != '') &&
($('.vita input[required]').val() != '') &&
($('.bacino input[required]').val() != '') &&
($('.braccio input[required]').val() != '') &&
($('.avambraccio input[required]').val() != '') &&
($('.polso input[required]').val() != '') &&
($('.camicia input[required]').val() != '')) {
$(".configure-product").show();
clearInterval(checkExist);
}
else {
$(".configure-product").hide();
}
}, 400);
});
Is it possible to make it more scalable?
Ciao Riccardo,
Are you looking for this ?
jQuery(document).ready(function($){
$(".wapf-wrapper").on("keyup", function() {
var filled = true;
$(".wapf-wrapper").each(function(){
if($(this).val() == '') {
filled = false
}
});
$(".configure-product").toggle(filled);
});
});
check on codepen

Internet Explorer stay on the current page after clicking cancel button

On Internet Explorer, i am using below code
if (navigator.appName == 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1)) {
window.onbeforeunload = function () {
//return "Ticket upload progress will be lost if you close/reload this window.\nDo you really want to continue?"
var message = confirm("Ticket upload progress will be lost if you close/reload this window.\nDo you really want to continue?");
if (message == true) {
alert(1);
}
else if(message == false) {
return;
}
}
}
When user click on ok or cancel in both the conditions, page is closed.
I want to stay on same page, if user clicked on cancel.
I have tried everything but not working.
Any help is highly appreciated.
Try to replace
else if(message == false) {
return;
}
with
else if(message === false) {
return null;
}

javascript Logical Operators don't do function when page equals to X

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)

JavaScript browser agent redirect

I've got this little bit of code on my webpage header to redirect the user to a different page if they are using an iPhone:
<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; }
</script>
Is there an easy way to 'reverse' this code, so anyone landing on the iPhone page from ANY other browser will be redirected back to the home page?
Many thanks.
Simple solution. Replace your existing one with this.
if(!/(iphone|ipod)/i.test(navigator.userAgent)) {
window.location = "Desktop.aspx";
}
Updated since its only for iPhone page.
To reverse? Replace != -1 with == -1
Edit:
Suppose you mean something like this:
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; } else {
document.location = "notiPhone.aspx";
}
(navigator.userAgent.indexOf('iPhone') == -1 means there is no such string "iPhone" in the userAgent. So when you change from != to ==, you need to change || to && as well.
if ((navigator.userAgent.indexOf('iPhone') == -1)
&& (navigator.userAgent.indexOf('iPod') == -1)) {
document.location = 'default.aspx';
}

Categories