Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not really a javascript guy and trying to get this simple image.src to change...
I have 3 mjpegs as sources that i want to alternate through when i click the image so far when i test on my webserver nothing happens with a click() when i test on JSFiddle.net it changes the image on the first click then nothing after that.
Any help appreciated.

HTML
<img id="mjpeg" src="http://ipaddress:8100/Mjpeg/0?authToken=abccb262-c400-4205-81e4-73ce28134d13" 
onclick="changeImage()"  width="704" height="480"/>

changeImage = function() {
    var image = document.getElementById('mjpeg');
                                                      
    if (image.src.match("/0?")) {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/1?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } 
    else if (image.src.match("/1?")) {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/2?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } 
    else if (image.src.match("/2?")) {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/0?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } <pre lang="Javascript">

What I have tried:

I have tried matching on the entire src string with no luck
Posted
Updated 6-Dec-19 11:33am

1 solution

I think i just figured it out by changing the matching function to == rather than .match

JavaScript
if (image.src == "http://ipaddress:8100/Mjpeg/0?authToken=abccb262-c400-4205-81e4-73ce28134d13") {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/1?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } 
    else if (image.src == "http://ipaddress:8100/Mjpeg/1?authToken=abccb262-c400-4205-81e4-73ce28134d13") {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/2?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } 
    else if (image.src == "http://ipaddress:8100/Mjpeg/2?authToken=abccb262-c400-4205-81e4-73ce28134d13") {
       image.setAttribute('src', "http://ipaddress:8100/Mjpeg/0?authToken=abccb262-c400-4205-81e4-73ce28134d13");
    } 
 
Share this answer
 
Comments
Visweswaran N 7-Dec-19 1:27am    
not completely related to your question but is it okay to expose a token like that in get request?
Member 9680117 25-Dec-19 13:00pm    
Yes, because its a public token with specific rights for public viewers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900