How to Access a JS variable in one clientlib to another clientlib | Community
Skip to main content
vineel_k
Level 2
December 29, 2022
Solved

How to Access a JS variable in one clientlib to another clientlib

  • December 29, 2022
  • 3 replies
  • 1451 views

I have a requirement that there is a 2 Clientlibs:

1)clientlibA ->clientlibA.js

2)clientlibB ->clientlibB.js

clientlibA.js

(function ($, $document, gAuthor) { "use strict"; var sampleText="sample-text-1"; })($, $(document), Granite.author);

How can i access sampleText variable in clientlibB.js

 

 

Thanks in advance!!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

you can use jQuery Extension Functions, which works as global functions for a request.

3 replies

Himanshu_Singhal
Community Advisor
Community Advisor
December 29, 2022

@vineel_k If both the clientlibs are invoked within the scope of same page then you can use JS window object to set the value and retrieve at another place (in another clientlib)

window.sampleText="sample-text.-1"

You've to make sure about the execution order i.e. clientlib A logic should execute before clientlib B else property value is going to be undefined. 

vineel_k
vineel_kAuthor
Level 2
December 30, 2022

hi @himanshu_singhal  clientlibA is presented in one dialog and clientlibB presented in another dialog

i tried above window object getting undefined in console

is there any way to access.

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
December 29, 2022

You can target the Javascript global scope. https://www.w3schools.com/js/js_scope.asp

 

vineel_k
vineel_kAuthor
Level 2
December 30, 2022

Hi @briankasingli i tried with global variables it is working in same dialog when i come back to another dialog in same page is is getting undefined but i need to access it in another dialog in same page 

 

 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
December 30, 2022

you can use jQuery Extension Functions, which works as global functions for a request.

Arun Patidar