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

How do I shorten an <img> source in JavaScript?

Currently I am making a game, and I want to check if my html element 'document.getElementById("terminal").src' is equal to "sprites/terminal.png"….
Because I use a live server via visual studio code, the "document.getElementById("terminal").src" is actually equal to 'http://127.0.0.1:5500/sprites/terminal.png'. Is there a way to change this or at least shorten the '.src' to exclude 'http://127.0.0.1:5500', and just be 'sprites/terminal.png'.
Thanks

>Solution :

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

Instead of the src property, you could also check the src attribute, which the browser does not rewrite to an absolute URL so it will be exactly as in the HTML source:

if (document.getElementById("terminal").getAttribute('src') === 'sprites/terminal.png') {
  // Do stuff
}
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