If you can use jQuery, it's quite simple, just invoke a function that creates the variable (keep in mind that you will only be able to operate with that variable inside that function), or if you need it anywhere else, create the variable at global scope and just use the function to modify it.For exmample:HTML:
<a id="page1" onclick="makevar('hello word')">Home</a><a id="page2" href="">About us</a><a id="page3" href="">Services</a><a id="page4" href="">Partners</a><a id="page5" href="">Contact us</a>
Javascript:
var var1="";function makevar(varcontent){var1=varcontent;alert(var1);}
See both options working here https://jsfiddle.net/3Lyeo6kc/1/ and https://jsfiddle.net/3Lyeo6kc/2/