Place your code in a(n anonymous) function and call said function when the element is clicked. For this, you will need to add an event listener to the element. This can be achieved using the
addEventListener function on an element.
More information about adding an event listener can be found here:
https://developer.mozilla.org/en-US/docs...ntListener
For a list of possible events, please take a look at the following page:
https://developer.mozilla.org/en-US/docs/Web/Events
For example, the function addEventListener can wait for a "click" event to occur:
Code
element.addEventListener("click", () => {
// Place your code here that needs to be executed after a click occurs
});
This is all very basic HTML and JavaScript. A live jsfiddle can be found here:
https://jsfiddle.net/cyLszejn/
This example uses an anonymous function, which has been possible since ES6:
https://jsfiddle.net/f19sv4ma/
Since I'm not doing anything with the event parameters, I have removed them from my example. But if you want, you can obviously work with event parameters like in this example:
https://jsfiddle.net/ke04nj15/
Code
element.addEventListener("click", (event) => {
// Work with event parameters, for example a simple: console.log(event);
});
I strongly recommend that you look into modern JavaScript development as well. More information about arrow functions can be found here:
https://developer.mozilla.org/en-US/docs..._functions
When you're using
elements without a href attribute,
you should not be using a a element in the first place. This is an extremely bad practice. Use other element types instead, like the
button that I showed you in the example or a
span. You can use CSS to change their appearance.
If instead, you want the link to change, when the site has finished loading, you could use something like this:
Code
const link = document.getElementById("aElementHere");
link.setAttribute("href", "http://google.com");
This will effectively set the
href attribute to be something else. A live example can be found here:
https://jsfiddle.net/ke04nj15/1/
Noted, this can seriously impact SEO, due to some search engines not being very clever in this regard.
More information regarding the
setAttribute function can be found here:
https://developer.mozilla.org/en-US/docs...tAttribute
You should also be aware that what you're trying to achieve (retrieving the operating system information) is not always possible and you might be faced with issues using your code. For example, some mobile browsers will provide you with incorrect information when it comes to the operating system and said information can also be modified by third parties. So all in all, if you wait for the DOM to be ready, and then 'correctly' identify the operating system and change the target URL, you should have what you're looking for. You should be able to do this on your own now with the information that was given to you in this post.
Some more information about JavaScript itself that you probably missed:
You should also look into
truthy and
falsy in JavaScript:
https://www.sitepoint.com/javascript-truthy-falsy/
One more thing. I noticed that you're using the
var keyword. ES6 has introduced
let and
const. You should learn when to use which identifier. More information can be found here, for modern JavaScript development:
https://tylermcginnis.com/var-let-const/