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 –  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>
TYeung
  • 2,579
  • 2
  • 15
  • 30
  • Care to elaborate as to why it is _very bad_? – pishpish Aug 31 '21 at 12:22
  • client.linkedin is already defined, when I use = client.linkedin ?> it works well. It´s just in the href that it´s not recognized – KB62 Aug 31 '21 at 13:27
  • @KB62 What does `client.linkedin` looks like? – TYeung Aug 31 '21 at 13:36
  • @KB62 Why would you use the php short tag to enclose a js variable? – TYeung Aug 31 '21 at 13:37
  • client.linkedin is what comes after https://www.linkedin.com/'THISPART. It is actually a google sheets cell which I get through a function. When using = client.linkedin ?> in my html code the output is the link itself. – KB62 Aug 31 '21 at 13:43
  • I just found the php short tag on internet and it worked. I'm ofc open to other suggestions – KB62 Aug 31 '21 at 13:45
  • When using the proposed solution, I only get LINKEDIN as an output without a link. Should I add href somewhere maybe? | LINKEDIN This would return https://www.linkedin.com/ as a link as if the variable is not understood – KB62 Aug 31 '21 at 13:49
  • @KB62 Use external js then – TYeung Aug 31 '21 at 14:28