Jump to content

JavaScript Variable as part of a tag


Recommended Posts

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!



Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.