Can I embed javascript as an xhtml attribute?
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
精彩评论