Checkout querySelector - javascript

I am relatively new to Javascript. I am trying to get the Price Value on our Checkout page.
document.querySelector returns different values if the checkout is made via Prepayment and Paypal.
Here is my code:
function() {
try {
var Prepayment = document.querySelector(".order-number>strong").innerText.trim();
var Paypal = document.querySelector(".checkout-success>p>span").innerText.trim();
if (document.querySelector(".order-number>strong") != null) {
return capturedText;
} else {
return Paypal;
}
} catch(e) {
return "";
}
}
Upon using the Tag manager, the Tag is giving me "" value.
Can someone help me how do i execute the queries with the QuerySelector within the If / Else environment, so does the function cannot block the entire script if my element which I am looking was not found.

Related

Javascript - replace loop with native js code problem for Google Tag Manager

Apologies in advance for my ignorance. I've searched the site, but haven't had any luck.
Rather than manually enter each hostname via GA's Admin Interface, utilize the following JS function in GTM to defines the list of exclusions (var referrals), create/compare incoming {{Referrer}} (.exec, .test methods), and then null the {{Referrer}} if it's on the list, or lets it pass unmodified to GA if no match is found:
function()
{
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
var hname = new RegExp('https?://([^/:]+)').exec({{Referrer}});
if (hname) {
for (var i = referrals.length; i--;) {
if (new RegExp(referrals[i] + '$').test(hname[1])) {
return null;
}
}
}
return {{Referrer}};
}
I sent the code to a developer for feedback, and he suggested replacing the for loop with with this (a direct replacement for the loop):
if (referrals.find(function(referral) { return hname[1].includes(referral); })) { return null; } else { return {{ Referrer }};
I attempted to do so like this:
function()
{
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
var hname = new RegExp('https?://([^/:]+)').exec({{ Referrer }});
if (hname) {
if (referrals.find(function(referral) { return hname[1].includes(referral); })) { return null; } else { return {{ Referrer }};
}
When attempting to publish this in GTM, I'm getting both parsing errors as well as unreferenced variable errors for {{Referrer}}.
If anyone has some feedback, I'd be super super grateful.
Uh... your developer doesn't know GTM's syntax. So now for this to work, it's either the developer needs to know GTM's variable syntax or for you to know JS, so I suggest you to use your old code. It's better to use the code you understand than the code you won't be able to maintain.
If you still wanna use it, try removing spaces from the variable reference.
And you forgot to close the else claus: else { return {{ Referrer }};} And one more time to close the external if... else { return {{ Referrer }};}}
And now it looks like a mess, so here, try this:
function() {
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
if (/https?:\/\/([^\/:]+)/i.test({{Referrer}})) {
if (referrals.find(function (referral) { return hname[1].includes(referral); })) { return null; } else { return {{Referrer}}; }
}
}

SailsJS V1 - Blueprint query not working correctly

I'm trying to create a blueprint query for a sailsjs v1 model.
The model is a BlogPost which has 2 "options". One is the target and the other one is Status.
If the target is Site and the status is Published, the query should return, otherwise nope. I'm using the default REST routes provided by Sails (blueprints) and everything works fine if I try to find all of them. However, if I try to find one by ID...I can't even get back those that have a status of 'Unpublished'.
This is my code in blueprint.js parseBlueprintOptions ->
parseBlueprintOptions: function (req) {
var queryOptions = req._sails.hooks.blueprints.parseBlueprintOptions(req);
if (queryOptions.using === 'blogpost') {
if (req.options.blueprintAction === 'create') {
queryOptions.newRecord.user_id = req.session.user.id;
return queryOptions;
} else if (req.options.blueprintAction === 'update') {
queryOptions.criteria.where.user_id = req.session.user.id;
queryOptions.valuesToSet.user_id = req.session.user.id;
return queryOptions;
} else {
if (req.session.administrator) {
return queryOptions;
} else {
queryOptions.criteria.where.blogpost_target = 'Site';
queryOptions.criteria.where.blogpost_status = 'Published';
console.log(queryOptions);
return queryOptions;
}
}
}
}
};
Any tips on why the query does not get triggered for findOne? As i said, it returns regardless of the status/target.
Hey 'find' returns object array and 'findone' returns only an object. I guess therefor it won't work!

Why aren't i successfully getting the embedded "if" in the second code section to do its job?

var checking_location = "none"
const getentitiesByType = (arr, type) => {
for (let i in arr) {
if (arr[i].type === type) {
checking_location = "exists"
return arr[i].entity
}
}
return null;
}
if (!meeting.location) {
if (checking_location != 'exists') {
rl.question('where is the location ', function(answer) {
// session.send("The location you gave:" answer);
rl.close();
session.send(answer)
// console.log(tryagain(answer, 'Calendar.Location'));
session.send(tryagain(answer, 'Calendar.Location'));
});
}
} else {
next();
}
What i'm trying to do here is to have a loop in the if (!meeting.location) if checking_location stays equal to none. Basically i want to check if a certain Json field exists, if it doesn't i want to keep asking the question in rl.question.My issues is that the code is only working the first time, then even if i give another input not containing the required field i don't get that question.Also note that this is not the entire code but it's more than enough to understand the possible issue spots in my implementation.
getentitiesByType needs to be called somewhere, simply assigning it to a variable will not make the function run: getentitiesByType(youArr, yourType).
Also, as a side note, instead of using string values for checking_location just rename the variable and use a boolean value. Ex: var hasLocation = false.

Equivalent of "GoTo" in Google Apps Script (equivalent VBA-GAS )

When writing my VBA macros I often used "GoTo" so as to jump to a previous part of the macro without leaving the Sub. Now that I’m converting all my macros to Google Apps Script I’m trying to find the equivalent for “GoTo”.
Sub MySub()
Dim sheetname1 As String
Dim sheetname2 As String
On Error GoTo Err
sheetname1 = ActiveSheet.Name
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "passwords"
sheetname2 = ActiveSheet.Name
GoTo aftererr
Err:
MsgBox Error(Err)
Exit Sub
aftererr:
This is just one instance of my use of GoTo. However I need it for my new scripts in many other ways; not just for redirecting errors. For example:
function MyFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
if(criteraA == criteraB){
sh.offset(1,0).activate();
var i=i + 1;
//?? GoTo ??
}else{
var i=0;
sh.getRange(row, column)(1,sr.offset(0,1).getColumn()).activate();
}
You don't need GoTo, most people would argue that it is terrible programming practice to use it even when it is present. Using other control structures will do the job.
if() {
} else if() {
} else {
}
for(;;) {
continue;
break;
}
while() {
}
do {
} while();
switch() {
case:
default:
}
// for errors
throw "Error string"
try {
} catch(error) {
}
You'll have to shuffle your logic around a bit, but it will result is better more maintainable code.

Appending to External Browser Window

I have a Windows app that contains a browser control that loads pages from my website. However, due to the Windows app, I cannot debug Javascript in the usual ways (Firebug, console, alerts, etc).
I was hoping to write a jQuery plug-in to log to an external browser window such that I can simply do something like:
$.log('test');
So far, with the following, I am able to create the window and display the templateContent, but cannot write messages to it:
var consoleWindow;
function getConsoleWindow() {
if (typeof (consoleWindow) === 'undefined') {
consoleWindow = createConsoleWindow();
}
return consoleWindow;
}
function createConsoleWindow() {
var newConsoleWindow = window.open('consoleLog', '', 'status,height=200,width=300');
var templateContent = '<html><head><title>Console</title></head>' +
'<body><h1>Console</h1><div id="console">' +
'<span id="consoleText"></span></div></body></html>';
newConsoleWindow.document.write(templateContent);
newConsoleWindow.document.close();
return newConsoleWindow;
}
function writeToConsole(message) {
var console = getConsoleWindow();
var consoleDoc = console.document.open();
var consoleMessage = document.createElement('span');
consoleMessage.innerHTML = message;
consoleDoc.getElementById('consoleText').appendChild(consoleMessage);
consoleDoc.close();
}
jQuery.log = function (message) {
if (window.console) {
console.log(message);
} else {
writeToConsole(message);
}
};
Currently, getElementById('consoleText') is failing. Is what I'm after possible, and if so, what am I missing?
Try adding
consoleDoc.getElementById('consoleText');
right before
consoleDoc.getElementById('consoleText').appendChild(consoleMessage);
If the line you added is the one that fails, then that means consoleDoc is not right, if the next line is the only one that fails then ..ById('consoleText') is not matching up
If I don't close() the document, it appears to work as I hoped.

Categories