I want to get specific links from my mail and open them in a new window.
I got this at the moment:
function OpenHrefsInNewWindow() {
//Get class name
var items = document.getElementsByClassName("confirm_link123");
var i = 0;
while(i < items.length)
{
//Get a part from the specific link
if (items[i].href.indexOf("confirm") > -1)
{
//If it does, open that URL in a new window.
window.open(items[i].href, "_blank");
}
i++; //Increment i here.
}
}
OpenHrefsInNewWindow();
But every time I get a new mail the classname is changing. So I got different classnames every time. So how can I do, to search only for the beginning of the class and change it in my code?
Any solutions? (:
Use class^=
syntax to find classes that start with
. To open multiple windows, generate a unique name instead of using '_blank'
.
const items = document.querySelectorAll('*[class^="confirm_link"]') console.log(items) document.querySelector('button').addEventListener('click', ()=>{ for(let i=0; i<items.length; i++){ console.log(items[i]) window.open(items[i].getAttribute('href'), 'window' + i) } })
<div id='container'> <div class = 'foobar' > <a class='confirm_link1' href='http://www.google.com'></a> <a class='confirm_link2' href='http://www.google.com'></a> <a class='confirm_link3' href='http://www.google.com'></a> </div> </div> <button>click me</button>
Your example should look like this:
function OpenHrefsInNewWindow() {
//Get class name
const items = document.querySelectorAll('*[class^="confirm_link"]')
var i = 0;
while (i < items.length) {
console.log('opening', items[i].getAttribute('href'))
window.open(items[i].getAttribute('href'), "window" + i);
i++; //Increment i here.
}
}
OpenHrefsInNewWindow();
as far as i know please use ES2015 code instead of ES2016.
var links = document.querySelectorAll('*[class^="confirm_link"]');
document.querySelector("button").addEventListener("click", function() {
for (var i = 0; i < links.length; i++) {
var _href = links[i].getAttribute("href");
if (_href.indexOf("confirm") > -1)
window.open(_href, "_blank");
}
});
use this may be this will be helpful
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.