KeithRules
KeithRules

Reputation: 207

Use absolute full path to the img instead of the local path

I use this snippet in my JavaScript code for preloading images:

my_path = "images/myimage.jpg";
my_image.src = my_path;

In this case, I have to use local path to the image as a value of my_path variable. If I use full path, I get error on the console, because the browser tries to load something like this:

http://www.mywebsite.com/http://www.mysebsite.com/images/myimage.jpg

How can I modify the snippet to be able to use absolute path to the image in it?

Upvotes: 2

Views: 144

Answers (1)

James Hill
James Hill

Reputation: 61793

Try:

my_path = "/images/myimage.jpg";

See Why would a developer place a forward slash at the start of each relative path? to understand the function of the forward slash in this context.

Upvotes: 1

Related Questions