Home-Assistant-Lovelace-HTML-Jinja2-Template-card icon indicating copy to clipboard operation
Home-Assistant-Lovelace-HTML-Jinja2-Template-card copied to clipboard

<script> in custom:html-template-card

Open tunerooster opened this issue 4 years ago • 7 comments

Is this the best place to ask questions re: this card? Please advise otherwise...

I have the following:

type: 'custom:html-template-card'
content: >
  <script>function hello() {alert('hi there');}</script>
  <button onclick="hello();"> O </button>

When I click the button nothing happens... Can I use javascript functions defined in

More specifically, I am needing to do these two things (among many others):

  • Popup up an mp4 video when a preview image is clicked.
  • Change the value of an input_text to refer to the "next" motion event image.

Thanks for your help!

tunerooster avatar Dec 01 '20 13:12 tunerooster

Unfortunately as far as I know it's not possible to use scripts in content. You can try to use picture-elements + browser mod to create a popup.

PiotrMachowski avatar Dec 01 '20 14:12 PiotrMachowski

You say, "as far as you know". You mean there is something outside your custom card which prevents it? I.e., it could not be done by any card?

P.S. Calling onclick='alert("hello")' works... ...just can't define/call a user defined function.

tunerooster avatar Dec 01 '20 15:12 tunerooster

Well, I am not an expert, but technology that is used in this card doesn't seem to like <script> in content, plus there is no access to HA functions from scripts created in context. It's possible that is doable, but I don't know how to do it ;)

PiotrMachowski avatar Dec 01 '20 16:12 PiotrMachowski

I was thinking of using the ha api for access to services... In fact, I might be able to do some things with href. I'll post any success...

tunerooster avatar Dec 02 '20 02:12 tunerooster

Is the

it can be really hard to keep straight when writing server-side script which generates client-side script ;-)

donburch888 avatar Mar 02 '23 10:03 donburch888

@donburch888 it is passed to the browser

PiotrMachowski avatar Mar 02 '23 11:03 PiotrMachowski

I found out that you actually can run a script, for example in image onLoad. Here I'm using a dummy image.

<img onLoad='
alert("Hello World!") 
' src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" />

PeterPalenik avatar Feb 07 '24 14:02 PeterPalenik