Jump to content

JavaScript Variable as part of a tag


melting_dog

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!

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/263567-javascript-variable-as-part-of-a-tag/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.