Using a global variable within HTML <body> section

I wish to insert a predefined (global) URL somewhere in a HTML file which can then be used elsewhere in the <body> section of the HTML file. Example:

<html>
<head>
</head>
<!-- define a global string somewhere before the body tag -->
<!-- Example:  GlobalURL = "http://mysite.com/aurl.htm" -->
<body>
<p>
I then wish to insert the <a href="%GlobalURL%">URL here</a>
</p>
</body>
</html>

Open in new window


How can I do this?
Monroe406Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
You can define the var in the head section and then to use it wherever you want:
<head> 
<title>HTML, CSS and JavaScript demo</title>
  <script>
  var globalUrl='http://mysite.com/aurl.htm';
  </script>
</head>

Open in new window

Leonidas DosasCommented:
And if we want to be more accurate define the var in the head section I showed above and the via script AFTER the variable definition set the value of the href:
<!DOCTYPE html>
<html>
<head> 
<title>HTML, CSS and JavaScript demo</title>
  <script>
  var globalUrl='http://mysite.com/aurl.htm';
  </script>
</head>
<body>
<!-- Start your code here -->

<p>
  <a href="" id="myLink">AAAA</a>
</p>
  <script>
  window.onload=function(){    
  document.getElementById('myLink').setAttribute('href',globalUrl);
 };
  </script>
<!-- End your code here -->
</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leonidas DosasCommented:
Be careful not to re-declare the variable AFTER its original definition-declaration because it will get the new value.
Monroe406Author Commented:
Unfortunately the code that was provided only works with Microsoft browsers: IE and Edge.  The code does not work with: Firefox, Chrome, Vivaldi, Aviator.
Leonidas DosasCommented:
What errors do you have in the console?(Press F-12)
At my chrome it works without any problem.
Capture.JPG
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.