gfr
gfr

Reputation: 25

Adjusting video src to URL parameter

I'd like to set the src to my video according to the URL parameter (after the ?)
Here's what I've got so far:

<video></video>
<script>
document.getElementsByTagName("video").src="###";

I need help with setting the ### src value to the correct value like so:
realistic example

https://website.com/player?@#$%

src="https://areallylongaddress.com/videosourcefolder/@#$%";

simpler example

https://website.com/player?intro_video

src="https://areallylongaddress.com/videosourcefolder/intro_video";

essentially, a strip of code that sets the video src to a fixed string value (areallylongaddress.com), then adds the url parameter to the end

Upvotes: 0

Views: 674

Answers (1)

Akshay Bistagond
Akshay Bistagond

Reputation: 84

If i understand you correctly u want to set the src of the video to the url parameter(query string). Usually query strings are link this - "https://website.com/player?srcName=intro_video" So all u need to do is

const queryString = window.location.search;
// querySting = ?srcName=intro_video
const urlParams = new URLSearchParams(queryString);

const src_name = urlParams.get('src_name');
console.log(src_name);
//prints "intro_video"
document.getElementsByTagName("video").src=src_name;

Edit -

      //main Html form where u go to https://website.com/player?folders/vkxq2f.mp4 page
    let vid_URL = "https://files.catbox.moe/vkxq2f.mp4";
    let vid_name = vid_URL.split('/').at(-1);
    console.log(vid_name);
    //here you do something like onclick(window.href=`https://website.com/player?video=${vid_name}`)

    //Then in player.html
    document.getElementsByTagName("video").src = new URLSearchParams(window.location.search).get('video');

Upvotes: 1

Related Questions