/    Sign up×
Community /Pin to ProfileBookmark

Making an external link using external javascript

I have an image on my webpage, when I click on it, it opens another website. This is the code: <a href=”https://www.pierrevin.com/ ” target=”_blank”><img align=”center” alt=”www.pierrevin.com Le site des étiquettes de vins” border=”0″ src=”../image/site_cognac-ton.jpg”></a>. I am trying to make it work via external javascript, but I can’t get it to work. I am very much a beginner. Here is what I thought should work:

The code in the BODY: <a href=”cognacton.js”><img align=”center” src=”../image/site_cognac-ton.jpg” alt=”www.cognac-ton Le site des étiquettes de cognac”></a>

The scriptline in the HEAD: <script src=”../css/js/cognacton.js”</script>

The text in cognacton.js: window.location.href=”https://www.pierrevin.com/”

When I click on the image ‘site_cognac-ton.jpg’ on my webpage, the result is a white page with the text of the js-file: window.location.href=”https://www.pierrevin.com/”. Why does it not open the www.pierrevin.com website?

Thanks in advance, Ton

to post a comment
CSSJavaScript

3 Comments(s)

ImageDavinci•003 auto-replyTo create an external link using external JavaScript, you can dynamically set the href attribute of an anchor (<a>) tag in your HTML. Here’s a step-by-step guide on how to do this:

1. **HTML Setup**: First, you need an anchor tag in your HTML where the link will be placed. You can assign it an id for easy access.

``html
<a id="myExternalLink">Click Here</a>
`

2. **External JavaScript File**: In your external JavaScript file, you will write the code to set the
href attribute of the anchor tag.

`javascript
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('myExternalLink');
link.href = 'https://www.example.com';
link.target = '_blank'; // Optional: Opens the link in a new tab
});
`

3. **Linking the JavaScript File**: Make sure to link the external JavaScript file in your HTML file. This should be placed inside the
tag or just before the closing tag.

`html
<script src="path/to/your/script.js"></script>
`

Here’s how your complete setup might look:

**HTML File (index.html):**
`html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External Link Example</title>
<script src="script.js"></script>
</head>
<body>
<a id="myExternalLink">Click Here</a>
</body>
</html>
`

**JavaScript File (script.js):**
`javascript
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('myExternalLink');
link.href = 'https://www.example.com';
link.target = '_blank'; // Optional: Opens the link in a new tab
});
``

This setup ensures that your link is dynamically set when the page loads, and since it's done through an external JavaScript file, it keeps your HTML clean and separates concerns effectively.

Was this helpful?

Copy linkTweet thisAlerts:
Image@AxyonorigauthorDec 21.2025 — Yes the above helps a bit, but I want it to happen when an image is clicked.
×

Success!

Help @Axyonorig spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 1.23,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @dert,
tipped: article
amount: 1000 SATS,

tipper: @viney352,
tipped: article
amount: 10 SATS,
)...