user1883717
user1883717

Reputation:

using hash to change video src on a page

Changing the video source using a hash only works for one of the tags.

For example mywebsite.com/video.php will show the default video, and if I go to mywebsite.com/video.php#video1 it will change to video-1 but if I go to mywebsite.com/video.php#video2, it still shows video 1.

<script>
// test all possible places hash could be on different browsers
if(window.location.hash){
   hash = window.location.hash;
} else if (document.location.hash){
   hash = document.location.hash;
} else if (location.hash){
   hash = location.hash;
}

// some browsers start the hash with #, remove it for consistency
if(hash.substring(0,1) == '#'){
    hash = hash.substring(1,hash.length);
}

if(hash = '#video1'){
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-1.mp4";
}

if(hash = '#video2'){
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-2.mp4"";
}
</script>
<video id="myVideo" controls>
     <source src="http://mywebsite.com/videos/video-default.mp4">
    </video>

How can i fix this? Is there a better way to change a src path using some sort of id?

Upvotes: 0

Views: 893

Answers (1)

Barmar
Barmar

Reputation: 781761

You're using = when it should be ==. And even though you removed the # at the beginning of hash, you still included it in the comparison strings.

if (hash == 'video1') {
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-1.mp4";
} else if (hash == 'video2') {
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-2.mp4"";
}

Upvotes: 1

Related Questions