开发者

Can I embed javascript as an xhtml attribute?

开发者 https://www.devze.com 2023-03-27 19:03 出处:网络
I want to update this code 开发者_运维知识库to call javascript in order to validate the url of my image.This is PHP echoing xhtml with embedded javascript.

I want to update this code 开发者_运维知识库to call javascript in order to validate the url of my image. This is PHP echoing xhtml with embedded javascript.

  echo "<img class = \"c\" src=\"$fav\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

This is what I have. Will it work?

  echo "<img class = \"c\" src=\"<script type=\"text/javascript\">validate_fav($fav)</script>\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

Thanks.


No, that is invalid XHTML. You end up setting the src to <script type= and then you have invalid markup.

You should do something like:

echo "<img id=\"theimg\" class = \"c\" src=\"$fav\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

?>

<script type="text/javascript">
    document.getElementById('theimg').setAttribute('src', validate_fave($fav));
</script>

<?php
0

精彩评论

暂无评论...
验证码 换一张
取 消