Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get string inside the src attribute not the absolute path

I have a video tag like this:

<video id="video-upload-preview" controls="" src="/admin/session/trimed/trimmed.mp4"></video>

and I want the string inside src so I did this:

document.getElementById('video-upload-preview').src

But the result is something else, it’s the absolute path to the source not the actual string:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://www.example.com/admin/session/trimed/trimmed.mp4&#8217;

Is there any way to get only the string inside src attribute?

The desired result would be:

/admin/session/trimed/trimmed.mp4

>Solution :

Try this:

function removeSubStr(){
    let domain = "https://stacksnippets.net"; // Here put "https://www.example.com"
    let str = document.getElementById("video-upload-preview")
                      .src.replace( domain, "" );
    document.getElementById("url").textContent = str;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <video id="video-upload-preview" controls="" src="/admin/session/trimed/trimmed.mp4"></video>
        <p id="url">Click button</p>
        <button onClick="removeSubStr()">Click</button>
    </body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading