Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

html - how to insert a picture using clear javascript?

Schematically what I want. How to do it?

<img src=incamingMessage>
<script>
  var socket = new WebSocket("ws://localhost:9090/");

  socket.onmessage = function(event) {
    var incomingMessage = event.data;
    insertPic(incomingMessage);
  };

  function insertPic(message) {
    //here something that change <img>
  };

  //event.data = "img/312.png"
</script>

update added listener. i dont know how to change arg in img


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

At first, who is event.data? What data type? What kind of variable? Where is it defined? Is it an array of bytes? is it a var?

What you do is probably better doable with PHP like this:

<?php

$incomingMessage = // populate with the actual data from your source.

echo '<img src="'.incomingMessage'">';

?>

As for Javascript, I have no idea what to tell you because you did not provide any info about the source of the data, the variable that stores it, etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...