melting_dog Posted June 3, 2012 Share Posted June 3, 2012 Hi all, A simple one but it seems I am running into trouble. All I want is to add a JavaScript variable to the html body as part of an <a> tag, eg: <a href="JAVASCRIPT VARIABLE">Click Me</a> Here what I have: <div class="headerrefresh"><script>document.write('<a href="'+pathname+'">Refresh</a>')</script></div> Any help woud be very appreciated! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/263567-javascript-variable-as-part-of-a-tag/ Share on other sites More sharing options...
Mijii Posted June 3, 2012 Share Posted June 3, 2012 Not sure if you have found the answer already but based on your above something like this will work <div class="headerrefresh"><script>var pathname = 'bob';</script><script>document.write('<a href="'+pathname+'">Refresh</a>')</script></div> basically you need to declare the var before using it then you can use it however you want later in the page. Quote Link to comment https://forums.phpfreaks.com/topic/263567-javascript-variable-as-part-of-a-tag/#findComment-1350776 Share on other sites More sharing options...
melting_dog Posted June 3, 2012 Author Share Posted June 3, 2012 Thanka Mijii I had declared the var up in my header but for some reason it only works this way. Quote Link to comment https://forums.phpfreaks.com/topic/263567-javascript-variable-as-part-of-a-tag/#findComment-1350778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.