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
4
Answers
You can take a look on
Get relative URL from absolute URL
For a general URL extraction.
in your case, you can also do the follow:
which will return the path without the origin, then you can assign it back to the src:
I think the most certain result would be received by using
URL()
constructor:You can use the
URL
class in JavaScript to get thepathname
.Instead of the
src
property, you could also check thesrc
attribute, which the browser does not rewrite to an absolute URL so it will be exactly as in the HTML source: