JavaScript code for is populated

Gold Contributor
Gold Contributor

Hello,

Can you tell me the exact code in JavaScript for check if a variable is populated?

Thank you in advance.

Javier Millán

5 REPLIES 5

JavaScript code for is populated

Moderator
Moderator
Hi @jmillan

Let's say your variable is called "page_name" and is a property of the "b" object:

e.g.

b.page_name="homepage";

To check if this is populated, you could just write:

if (b.page_name) {
console.log("page name is populated with" + b.page_name);
}

This will check that:

a) page_name is defined
b) page_name is not equal to an empty string
c) page_name is not equal to the integer value of 0 (zero)

If you would like to be more explicit in your check, you can do:

if (b.page_name !== undefined && b.page_name !== "") {
console.log("page name is populated with" + b.page_name);
}

I hope this helps.

Craig.
Check out our new Swift integration library for iOS, macOS, tvOS and watchOS: https://github.com/Tealium/tealium-swift with updated
documentation https://community.tealiumiq.com/t5/Swift/tkb-p/swift.

JavaScript code for is populated

Gold Contributor
Gold Contributor

Thank you very much Craig!

JavaScript code for is populated

Gold Contributor
Gold Contributor
@craig_rouse

If you type in a console b.page_name="homepage, i presume you would get b is not defined. In that case, please go ahead with utag.data.page_name value in Console.

Correct me if my understanding is misstated. Thanks!

JavaScript code for is populated

Moderator
Moderator
Hi @Srinivasan,

Yes, after the page has loaded, the "b" object is no longer defined, and utag.data is the value you would use to check the value of the page_name variable. However, inside an extension in TiQ, you should always reference the "b" object, and never utag.data (unless you are using the "DOM Ready" scope, in which case, the "b" object does not exist, and you should use utag.data instead.

I hope this is clear.
Check out our new Swift integration library for iOS, macOS, tvOS and watchOS: https://github.com/Tealium/tealium-swift with updated
documentation https://community.tealiumiq.com/t5/Swift/tkb-p/swift.

JavaScript code for is populated

Gold Contributor
Gold Contributor
@craig_rouse

Thank you for you prompt response.

Yes you are correct.

I will referring this file when i used b object and scope it to DOM Ready.

https://zjzhh79394.i.lithium.com/t5/image/serverpage/image-id/3283iBF86A3E7883443AC/image-size/origi...
Public