Rusly - Mices
Rusly - Mices

Reputation: 195

How to add domain url in image src using jquery or javascript?

I want to add a domain url to an image src without replacing the image name:

Domain Url : http://www.example.com

Before:

<img src="images/old.gif">

After:

<img src="http://www.example.com/images/old.gif">

I have tried using javascript but no luck:

document.getElementById("footer").getElementsByTagName("img")[0].src = "http://www.example.com";

Upvotes: 0

Views: 3190

Answers (1)

Josh
Josh

Reputation: 12566

$("#footer > img").attr("src", "http://site.com/" + $("#footer > img").attr("src") );

Or, you might try check out the base tag:

<head>
    <base href="http://www.w3schools.com/images/" target="_blank" />
</head>

<body>
    <img src="stickman.gif" />
    <a href="http://www.w3schools.com">W3Schools</a>
</body>

Upvotes: 1

Related Questions