Verifying Referral URL String & Cookie String with Javascript IF - javascript

I am attempting to verify the referral URL and whether or not a cookie contains a certain string and then perform an action, however right now - my IF statement is always TRUE.
When I remove the ref1 section, and only keep getcookie, I believe the statement is false; although then my else if does not work
Nevertheless, I believe the construction of this If statement is missing something.
if (ref1.indexOf('ccov') || ref1.indexOf('top10') || ref1.indexOf('cenf') || ref1.indexOf('aip')||
ref1.indexOf('constructioncoverage')||
ref1.indexOf('aginginplace')|| ref1.indexOf('seniorliving')|| ref1.indexOf('seniorlist') ||
((getcookie('track-page-1').indexOf('ccov')>-1) > -1) ||
((getcookie('track-page-1').indexOf('top10')>-1) > -1) ||
((getcookie('track-page-1').indexOf('cenf')>-1) > -1) ||
((getcookie('track-page-1').indexOf('aip')>-1) > -1)) {
Complete Code if Interested:
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
var userLang = navigator.language || navigator.userLanguage;
s1.async=true;
var country = ('; '+document.cookie).split('; country=').pop().split(';')[0];
var ref1 = document.referrer;
// if (['BS'].indexOf(country) > -1){
// s1.src='https://embed.tawk.to/61488c2325797d7a89ffe3dc/1fg1m6qod';
// }
// else if (country == 'MX') {
// s1.src='https://embed.tawk.to/6148c99f25797d7a89ffef5b/1fg2576ci';
// }
if (ref1.indexOf('ccov') || ref1.indexOf('top10') || ref1.indexOf('cenf') || ref1.indexOf('aip') || ref1.indexOf('constructioncoverage') || ref1.indexOf('aginginplace') || ref1.indexOf('seniorliving') || ref1.indexOf('seniorlist') || ((getcookie('track-page-1').indexOf('ccov')>-1) > -1) || ((getcookie('track-page-1').indexOf('top10')>-1) > -1) || ((getcookie('track-page-1').indexOf('cenf')>-1) > -1) || ((getcookie('track-page-1').indexOf('aip')>-1) > -1)) {
s1.src='https://embed.tawk.to/620675459bd1f31184dc28c0/1frkjk5mj';
}
else if (userLang.match(/^es/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1f5dpa29q';
}
else if (userLang.match(/^pt/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1f63jmv3f';
}
else if (userLang.match(/^fr/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1fa3s0cbv';
}
else if (userLang.match(/^ru/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1fa3rv0no';
}
else if (userLang.match(/^lv/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1fa3tcrog';
}
else if (userLang.match(/^et/)) {
s1.src='https://embed.tawk.to/609a845c185beb22b30c3bf3/1fa3tv7ll';
}
else if (userLang.match(/^lt/)) {
s1.src='https://embed.tawk.to/60e765f7649e0a0a5ccb3f41/1fa3uck2u';
}
else if (userLang.match(/^sv/)) {
s1.src='https://embed.tawk.to/60e765f7649e0a0a5ccb3f41/1fa5mfs8d';
}
else if (userLang.match(/^de/)) {
s1.src='https://embed.tawk.to/60e765f7649e0a0a5ccb3f41/1fa5muuvn';
}
else if (userLang.match(/^no/)) {
s1.src='https://embed.tawk.to/60e765f7649e0a0a5ccb3f41/1fa5ncduf';
}
else if (userLang.match(/^fi/)) {
s1.src='https://embed.tawk.to/60e765f7649e0a0a5ccb3f41/1fa5nudj7';
}
else if (userLang.match(/^nl/)) {
s1.src='https://embed.tawk.to/60e8531a649e0a0a5ccb61ee/1fa5o97t4';
}
else if (userLang.match(/^da/)) {
s1.src='https://embed.tawk.to/60e8531a649e0a0a5ccb61ee/1fa5orip4';
}
else if (userLang.match(/^el/)) {
s1.src='https://embed.tawk.to/60e8531a649e0a0a5ccb61ee/1fa5pkm1k';
}
else if (userLang.match(/^it/)) {
s1.src='https://embed.tawk.to/60e8531a649e0a0a5ccb61ee/1fa5q4uh2';
}
else if (userLang.match(/^pl/)) {
s1.src='https://embed.tawk.to/60e8531a649e0a0a5ccb61ee/1fa5qj4r7';
}
else if (userLang.match(/^cs/)) {
s1.src='https://embed.tawk.to/60e85defd6e7610a49aa78ec/1fa5qtrst';
}
else if (userLang.match(/^ro/)) {
s1.src='https://embed.tawk.to/60e85defd6e7610a49aa78ec/1fa5rl5gg';
}
else if (userLang.match(/^be/)) {
s1.src='https://embed.tawk.to/60e85defd6e7610a49aa78ec/1fa5rvbod';
}
else if (userLang.match(/^uk/)) {
s1.src='https://embed.tawk.to/60e85defd6e7610a49aa78ec/1fa5sgafc';
}
else if (userLang.match(/^hu/)) {
s1.src='https://embed.tawk.to/60e85defd6e7610a49aa78ec/1fa5t38bi';
}
else if (userLang.match(/^hr/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa5to1mj';
}
else if (userLang.match(/^sr/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa5u9emd';
}
else if (userLang.match(/^ka/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa5uus6k';
}
else if (userLang.match(/^sk/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa5vdsc3';
}
else if (userLang.match(/^tr/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa60012u';
}
else if (userLang.match(/^ar/)) {
s1.src='https://embed.tawk.to/60e8697ad6e7610a49aa7abc/1fa60cgb7';
}
else {
s1.src='https://embed.tawk.to/60a7b623b1d5182476bb3457/1f67huun6';
}
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();

It's tricky to debug this code as a sample of the value of ref1 is not provided. However, I can give some advice on where to go with this.
The indexOf method returns either the index of the substring/element provided, or -1 if not present. Be aware that Boolean(-1) returns true.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
document.referrer is a string, so you should probably make use of the includes method on string. This properly yields true when the substring is present, and false when it is not.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
Try replacing the indexOf methods on ref1 with includes like so:
if (ref1.includes('ccov') || ref1.includes('top10') || ref1.includes('cenf') || ref1.includes('aip') ||
ref1.includes('constructioncoverage') ||
ref1.includes('aginginplace') || ref1.includes('seniorliving') || ref1.includes('seniorlist') ||
((getcookie('track-page-1').indexOf('ccov') > -1) > -1) ||
((getcookie('track-page-1').indexOf('top10') > -1) > -1) ||
((getcookie('track-page-1').indexOf('cenf') > -1) > -1) ||
((getcookie('track-page-1').indexOf('aip') > -1) > -1)) {
}

Related

How to return return boolean false if strings has empty spaces

How to check for blank space in javascript.
My code:
function checkSubstring(text, part) {
for (let i = 0; i <= text.length; i++) {
if (text.toLowerCase().indexOf(part.toLowerCase()) !== -1 || part.toLowerCase().indexOf(text.toLowerCase()) !== -1){
return true;
}
else if (text === ' ' || part === ' ') {
return false
}
}
return false
}
console.log(checkSubstring(' ', ' '));
I tried check if white space using indexOf(' '), check if strings === 0 but it always return true.
Change your function as below
function checkSubstring(text, part) {
for (let i = 0; i <= text.length; i++) {
if (text.trim().length === 0 || part.trim().length === 0) {
return false
}
if (text.toLowerCase().indexOf(part.toLowerCase()) !== -1 || part.toLowerCase().indexOf(text.toLowerCase()) !== -1){
return true;
}
}
return false
}

React JavaScript How to optimize this if / or statement

Do you have a better way to create this if / elseif sentence?
if ((navigator.userAgent.indexOf('Opera') || navigator.userAgent.indexOf('OPR')) != -1) {
this.state.isBrowserAllowed = true;
} else if (navigator.userAgent.indexOf('Chrome') != -1) {
this.state.isBrowserAllowed = true;
} else if (navigator.userAgent.indexOf('Safari') != -1) {
this.state.isBrowserAllowed = true;
} else if (navigator.userAgent.indexOf('Firefox') != -1) {
this.state.isBrowserAllowed = true;
}
I'd use a regular expression instead.
this.setState({
...this.state,
isBrowserAllowed: /Opera|OPR|Chrome|Safari|Firefox/.test(navigator.userAgent)
});
Also, as the comment notes, state should not be mutated in React - use setState instead.

Getting an Unknown CORS error in Odoo-12 POS UI Book Orders

I am getting this error on POS UI I really need help because I don't understand the error
I am using the Book Order module and I often get this error like 1 in 10 book orders
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at child.add_product (point_of_sale.assets.js:493)
at Class.line_select (point_of_sale.assets.js:429)
at HTMLButtonElement.<anonymous> (point_of_sale.assets.js:425)
at HTMLTableSectionElement.dispatch (web.assets_common.js:892)
at HTMLTableSectionElement.elemData.handle (web.assets_common.js:865)
here's how I modified the add_product function
add_product: function(product, options){
var can_add = true;
var changes = this.pos.get_order();
var self = this;
if(changes.selected_orderline){
if(changes.selected_orderline.order){
if(changes.selected_orderline.order.quotation_ref){
if(changes.selected_orderline.order.quotation_ref.book_order){
can_add= false;
}
}
}
}
if (can_add){
if(this._printed){
this.destroy();
return this.pos.get_order().add_product(product, options);
}
this.assert_editable();
options = options || {};
var attr = JSON.parse(JSON.stringify(product));
attr.pos = this.pos;
attr.order = this;
var line = new POSInheritmodel.Orderline({}, {pos: this.pos, order: this, product: product});
if(options.quantity !== undefined){
line.set_quantity(options.quantity);
}
if(options.price !== undefined){
line.set_unit_price(options.price);
}
//To substract from the unit price the included taxes mapped by the fiscal position
this.fix_tax_included_price(line);
if(options.discount !== undefined){
line.set_discount(options.discount);
}
if(options.discounted_amount !== undefined){
line.set_discount_amount(options.discounted_amount);
}
if(options.discount_percentage !== undefined){
line.set_if_discount_percentage(options.discount_percentage);
}
if(options.discount_amount !== undefined){
line.set_if_discount_amount(options.discount_amount);
}
if(options.extras !== undefined){
for (var prop in options.extras) {
line[prop] = options.extras[prop];
}
}
if(options.uom !== undefined || options.uom !== false){
line.set_unit_id(options.uom);
}
if(options.is_board_feet !== undefined && options.is_board_feet){
line.set_is_board_feet(true);
line.set_length(product.length);
line.set_thickness(product.thickness);
line.set_width(product.width);
// console.log(product.length);
}else if(options.is_board_feet !== undefined && !options.is_board_feet){
line.set_is_board_feet(false);
}else{
if(changes.attributes.client !== null){
if(changes.attributes.client.is_wholesale || changes.attributes.client.is_retail){
if(product.is_board_feet){
line.set_is_board_feet(true);
line.set_length(product.length);
line.set_thickness(product.thickness);
line.set_width(product.width);
}
}
}
}
if(options.is_lineal_feet && options.is_lineal_feet !== undefined){
line.set_is_lineal_feet(true);
line.set_length(product.length);
// console.log(product.length);
}else if(options.is_lineal_feet !== undefined && !options.is_lineal_feet){
line.set_is_lineal_feet(false);
}else{
if(changes.attributes.client !== null){
if(changes.attributes.client.is_wholesale || changes.attributes.client.is_retail){
if(product.is_lineal_feet){
line.set_is_lineal_feet(true);
line.set_length(product.length);
}
}
}
}
if(options.is_lineal_meter && options.is_lineal_meter !== undefined){
line.set_is_lineal_meter(true);
line.set_length(product.length);
// console.log(product.length);
}else if(options.is_lineal_meter !== undefined && !options.is_lineal_meter){
line.set_is_lineal_meter(false);
}else{
if(changes.attributes.client !== null){
if(changes.attributes.client.is_wholesale || changes.attributes.client.is_retail){
if(product.is_lineal_meter){
line.set_is_lineal_meter(true);
line.set_length(product.length);
}
}
}
}
if(options.with_base_price){
line.set_with_base_price(true);
if(options.base_price_discount !== undefined){
line.set_base_price_discount(options.base_price_discount);
}
if(options.markup !== undefined){
line.set_markup(options.markup);
}
// console.log(product.length);
}
if(options.is_weight !== undefined && options.is_weight){
line.set_is_weight(true);
line.set_weight_receipt_display(options.base_weight);
}else if(options.is_weight !== undefined && !options.is_weight){
line.set_is_weight(false);
if(options.base_weight !== undefined && options.base_weight != 0){
line.set_weight_receipt_display(options.base_weight);
}else{
line.set_weight_receipt_display(product.weight);
}
}else{
if(options.base_weight !== undefined && options.base_weight != 0){
line.set_weight_receipt_display(options.base_weight);
}else{
line.set_weight_receipt_display(product.weight);
}
if(changes.attributes.client !== null){
if(changes.attributes.client.is_wholesale || changes.attributes.client.is_retail){
if(product.is_weight){
line.set_is_weight(true);
}
}
}
}
var to_merge_orderline;
for (var i = 0; i < this.orderlines.length; i++) {
if(this.orderlines.at(i).can_be_merged_with(line) && options.merge !== false){
to_merge_orderline = this.orderlines.at(i);
}
}
if (to_merge_orderline){
to_merge_orderline.merge(line);
} else {
this.orderlines.add(line);
}
this.select_orderline(this.get_last_orderline());
if(line.has_product_lot){
this.display_lot_popup();
}
}else{
self.pos.gui.show_popup('error',{
title :_t('Modification Restricted'),
body :_t('Modification is not allowed for booked orders.'),
});
}
},
this is all that I can give I didn't try anything to fix this because I don't really know where to start any help is very much appreciated thank you.

How to replace cross browser javascript to jQuery

Here is my code I have written - javascript code for IE and Chrome.
How can replace this with jQuery?
All this code working fine with the Javascript but I want replace it with jQuery.
Please help me to resolve this issue
function GetObject(objName)
{
var objReturn;
if (navigator.appName == 'Microsoft Internet Explorer')
{
var x = top.document.forms.Form1.ownerDocument.getElementById("iframe_module_details");
var y = (x.contentWindow || x.contentDocument);
if (y.document && y.document.getElementById(objName))
return y.document.getElementById(objName)
if (window.Form1 && window.Form1.document.getElementById(objName)) {
objReturn = window.Form1.document.getElementById(objName);
}
else if (window.parent.document.forms[0].document.getElementById(objName))
{
objReturn = window.parent.document.forms[0].document.getElementById(objName);
}
else if (top.frames[0].document.forms[0].document.getElementById(objName))
{
objReturn = top.frames[0].document.forms[0].document.getElementById(objName);
}
else if (window.parent && window.parent.Form1 && window.parent.Form1.document.getElementById(objName)) {
objReturn = window.parent.Form1.document.getElementById(objName);
}
else if (opener) {
if (opener.opener) {
if (opener.opener.parent.Form1 && opener.opener.parent.Form1.document.getElementById(objName)) {
objReturn = opener.opener.parent.Form1.document.getElementById(objName);
}
}
else if (opener.parent && opener.parent.Form1 && opener.parent.Form1.document.getElementById(objName)) {
objReturn = opener.parent.Form1.document.getElementById(objName);
}
}
}
else { //For Chrome
if (window.Form1 && window.Form1.document.getElementById(objName)) {
objReturn = window.Form1.document.getElementById(objName);
}
else if (document.getElementById(objName))
{
objReturn = window.parent.document.forms[0].document.getElementById(objName);
}
else if (document.getElementById(objName))
{
objReturn = top.frames[0].document.forms[0].document.getElementById(objName);
}
else if (window.parent && window.parent.Form1 && window.parent.Form1.document.getElementById(objName)) {
objReturn = window.parent.Form1.document.getElementById(objName);
}
else if (opener) {
if (opener.opener) {
if (opener.opener.parent.Form1 && opener.opener.parent.Form1.document.getElementById(objName)) {
objReturn = opener.opener.parent.Form1.document.getElementById(objName);
}
}
else if (opener.parent && opener.parent.Form1 && opener.parent.Form1.document.getElementById(objName)) {
objReturn = opener.parent.Form1.document.getElementById(objName);
}
}
}
//alert(objReturn);
return objReturn;
}
I don't want multiple code for different browser
Your code is very big for this simple task.
If I read correctly your code, it seems to always use getElementById, so it's very easy with jQuery.
So you can replace all this code with this function :
function GetObject(objName) {
return $('#'+objName).get(0); // Return the DOM element of objName, same as document.getElementById(objName)
}

What is simpler way to achieve an if then else if else stack in javascript

Is there a way to simplify this or a more terse form i can use? The logic contained is all correct. It just seems like a lot of returns and else ifs.
mode = (function(mode, current, proposed, origins, destinations) {
if (mode === 'none') {
return 'project';
} else if (proposed.count === 0) {
return 'unseated';
} else if (current.count > proposed.count && proposed.count > 0) {
return 'reducing';
} else if (proposed.count === destinations.count && destinations.count > 1 && current.count === 0) {
return 'newplus';
} else if (proposed.count === destinations.count && current.count === 0) {
return 'new';
} else if (proposed.count > destinations.count) {
return 'increasing';
} else if (proposed.count === destinations.count && destinations.count > origins.count) {
return 'moveplus';
} else {
return 'move';
}
}(moves.register[staff].move, {count: mode.currentDesks}, {count: mode.proposedDesks}, {count: mode.origins}, {count: mode.destinations})));
I previously used a nested ternary which is marginally shorted but i would argue more prone to error and difficult to read (the results are marginally different due to code evolution not errors in replication):
mode =
(moves.register[staff].move === 'none') ? 'project' :
(mode.proposedDesks === 0) ? 'unseated' :
(mode.currentDesks > mode.proposedDesks && mode.proposedDesks > 0) ? 'reducing' :
(mode.proposedDesks === mode.destinations && mode.destinations > 1 && mode.currentDesks === 0) ? 'newplus' :
(mode.proposedDesks === mode.destinations && mode.currentDesks === 0) ? 'new' :
(mode.proposedDesks > mode.destinations) ? 'additional' :
(mode.proposedDesks === mode.destinations && mode.destinations === mode.origins) ? 'move' :
(mode.proposedDesks === mode.destinations && mode.destinations > mode.origins) ? 'moveplus' :
'other';
So while I like the if…then…elseif stack for legibility, it feels like it is more verbose than it could be. I don't think i'm looking for a switch…case version doesn't quite cut it due to the number of comparison variables, and it feels wrong to nest if statements within a switch…case or ternary operators within an if…then…else.
I think instinctively i'd like a form of matrix where the return values are in a grid and somehow a matrix calculation of the the various bitwise conditions returned the correct result. I suspect though that would be a win of compact code over legibility.
Any suggestions?
NB. The variable names including the addition of a count property to each instead of naming the variable as such or not indicating that it is a count are chosen for legibility.
you can use a var to store your choice and return it at the end
mode = (function(mode, current, proposed, origins, destinations) {
var varName='move';
if (mode === 'none') {
varName='project';
} else if (proposed.count === 0) {
varName='unseated';
} else if (current.count > proposed.count && proposed.count > 0) {
varName= 'reducing';
} else if (proposed.count === destinations.count && destinations.count > 1 && current.count === 0) {
varName= 'newplus';
} else if (proposed.count === destinations.count && current.count === 0) {
varName='new';
} else if (proposed.count > destinations.count) {
varName= 'increasing';
} else if (proposed.count === destinations.count && destinations.count > origins.count) {
varName= 'moveplus';
}
}(moves.register[staff].move, {count: mode.currentDesks}, {count: mode.proposedDesks}, {count: mode.origins}, {count: mode.destinations})));

Categories

Resources