Question about Javascript variable

Question about Javascript variable

Hi, 

I'm new to Javascript so wondering if you can validate that this could work properly. 

On an A/B test page variation, i need to include a link where the href is dynamic replaced, depending on the page visited. I intend to ask the developer to add a javascript variable with the value that I need, then I can use the variable at pageSense test. 

I would ask him do add this line of code: 

  1. <script>
  2. var buttonUrl = "http://www.test.com/a";
  3. </script>

Then, at pageSense JS editor, I would add a code like this: 

  1. document.getElementById("bookNowABTest").href = buttonUrl;

... to replace the #bookNowABTest href value to the buttonUrl variable value. 

Would that work properly? 

Concern: 
* I'm firing the Asynchronous script version over Google Tag Manager. Does that means that there's a chance that the variable buttonUrl is not defined when page sense script is loaded, causing my href to not be replaced properly to the correct value? 

Thanks!