0

I have read many posts on adding a javascript variable in html href including document.write and onclick but nothing seems to work. I am working on google sheets script editor. My javascript url variables which are available in a script consists of client.linkedin (/.......), client.facebook (/.......)... I would need in my html:<a href=client.linkedin > | LINKEDIN</a>

I have tried: <a href="https://www.linkedin.com/" onclick="location.href=this.href+client.linkedin;return false;" > | LINKEDIN</a>

Starting with the javascript variable, I use a function which returns the info needed from google sheets in specific cell:

var client = {
        role: Getinfo("person1"),
        linkedin: Getinfo("person1"),
        number : Getinfo("person1"),
      };
var templ = HtmlService.createTemplateFromFile('HTMLFILE');
templ.client = client;

Therefore, the linkedin URL would change for every person. Going to the html file, what I usually did was:

<p> <strong><?= client.linkedin ?> </strong></p>

and it works perfectly for everything except the href tag. I don't mind switching methodology but that's what I found for now.

If anyone could help or tell me what Im doing wrong. Thanks

KB62
  • 1
  • 2
  • Here's how to do this properly with regular JavaScript (which is absolutely not the same as Java btw.): https://jsfiddle.net/9js3tp12/ Not sure if something like that will work for in a google-sheets context though – ChrisG Aug 31 '21 at 11:39
  • Please show full example code. Right now I only see that you put your JS code to string, so it will never work – Justinas Aug 31 '21 at 11:44
  • Does this answer your question? [How can I do string interpolation in JavaScript?](https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript) – Justinas Aug 31 '21 at 11:45

1 Answers1

0

client = {}
client.linkedin = "path/to/your/file"
document.getElementById("linkedin").href=client.linkedin;
<html>
  <body>
    <a id="linkedin"> | LINKEDIN</a>
  </body>
</html>
Learning Mathematics
  • 2,024
  • 2
  • 12
  • 26