简体   繁体   中英

How to get a redirected link with javascript

Given an array/list of URLs when iterating each link and opening each one up. I came across an issue which is, that some links like https://someLink.com/ONE get's redirected to https://someLink.com/TWO when clicked.

I iterate each link in my array and open it in the browser using "window.open()" function. and I want each link to be the last possible link, which means no redirections after I clicked.

document.getElementById("someBtnId").onclick = function(e) {
    e.preventDefault();
    for (let i = 0; i < urlLinks.length; i++) {
        window.open(urlLinks[i]); // here I want to open the real link.
    }
};

The catch is that I want to achieve this using only vanilla java script (if that's even possible).

EDIT: basically I want some button to open all the links I have in some arbitrary array (when clicked).

You need to assign a name to each window that you open

window.open(urlLinks[i], `Name${i}`);

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM