Links in JS

A

Anonymous

Guest
Hello!

When it comes to inserting links via Javascript, we usually use 2 different methods, but we are not specialists in this. We normally use the following methods:


Which of the 2 is more suitable to implement, are there other ways to introduce the link via JS?

Thank you very much and best regards.
 
Last edited by a moderator:
Is there any reason you can't just use proper <A> link elements? These will work much more predictably for users with screen readers or out of date devices.
 
Hello!

When it comes to inserting links via Javascript, we usually use 2 different methods, but we are not specialists in this. We normally use the following methods:


Which of the 2 is more suitable to implement, are there other ways to introduce the link via JS?

Thank you very much and best regards.

Both methods you've mentioned achieve the same result of redirecting the user to a different URL when the <span> element is clicked. However, there are slight differences between the two methods:

  1. window.location.href: This method changes the current location of the browser to the specified URL. It's commonly used when you want to replace the current page with a new one.

    <span onclick="window.location.href='https://www.domain.com/'">Anchor</span>


  2. window.open(): This method opens a new browser window or tab with the specified URL. The _self parameter tells the browser to open the URL in the current tab or window.

    <span onclick="window.open('https://www.domain.com/','_self')">Anchor</span>

    • If you want to replace the current page with the new URL, the first method (window.location.href) is more suitable.
    • If you want to open the new URL in a new tab or window within the same browser session, the second method (window.open()) is more suitable.
    Best Regard
    Danish Hafeez | QA Assistant
    ICTInnovations
 
Last edited by a moderator:
Back
Top