Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Open all links in new window Date: Thu, 04 Aug 2016 15:28:47 +0100 Organization: A noiseless patient Spider Lines: 80 Message-ID: <87wpjwsj4w.fsf@bsb.me.uk> References: <59e0b44d-8ea8-431d-ae12-e7585917f23f@googlegroups.com> <8737mku9we.fsf@bsb.me.uk> <7956cde6-c641-485f-9144-e7ce529f0cd8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="12159"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1894shLXotCmavMGFO5eomNHQ1BY4TJ7Iw=" Cancel-Lock: sha1:Rz20TPm5PCbgs26hK1fywXB0s54= sha1:mdSrYlOzpAYIml8Nb+KSYJsUH1s= X-BSB-Auth: 1.e07d3dd2d0a728184ff1.20160804152847BST.87wpjwsj4w.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:31058 Chandramohan Akkoni writes: > On Thursday, August 4, 2016 at 3:35:28 PM UTC+5:30, Ben Bacarisse wrote: >> Chandramohan Akkoni writes: >> >> > On Wednesday, July 27, 2016 at 2:15:10 PM UTC+5:30, Chandramohan >> > Akkoni wrote: >> >> Dear Friends, >> >> >> >> I have a website www.xyzabc.com (dummy site mentioned) and on the >> >> tutorials web page I have 100+ urls. Few assets are linked to my >> >> site and few are 3rd party links. I need a script which excludes my >> >> links and all 3rd party links are opened in a new tab even if I do >> >> not add target="_blank" or onclick().The Code should not be applied >> >> manually on all links, it should be one simple code. >> >> >> >> Thanks in advance. >> >> >> >> Thanks, >> >> Chandramohan >> > >> > I have the below code which work for small caps .pdf but does not work for .PDF and also I'm unable to check for .doc, .docx, etc. >> > >> > ---------------------------------------------------------------- >> > var pdfAnchors = document.querySelectorAll("a[href$='.pdf']"); >> > for(var i = 0; i < pdfAnchors.length; i++) { >> > pdfAnchors[i].target = pdfAnchors[i].target || "_blank"; >> > } >> > >> > ---------------------------------------------------------------- >> > Could you please check and get me the solution. >> >> The most flexible solution is to go back to the regular expression >> example (i.e. not using document.querySelectorAll) and change the match >> to include all the links you care about. You can match them all in one >> expression something like /^http|\.(pdf|doc|docx)$/i (untested). >> >> But if you can rely on up-to-date browser support, you can use case >> insensitive CSS attribute matching introduced in CSS4: >> >> a[href$='.pdf' i] >> >> which you'd combine with either having multiple (comma separated) >> selectors in querySelectorAll, or making multiple calls to the function. > > Thanks, this is working fine for small and big caps, but its unable to > take the link that are appended with some ?src codes, which we use for > analytics. It seems the first question was the tip of an iceberg -- no not just external links but links and pdfs and docs. No, not just pdfs but in any case. No, not just those but something else using ?src= as well. Each refinement has the potential of making the current solution less that idea. As some point another approach may well be better, so it would help if you can back up and list, as best you can, the full set of elements you want to find. Take care that there may also be elements you *don't* want to include despite them sharing some pattern in common with those you do. > Can anything be do this "a[href$='.pdf' i]", I tried adding /g after > i, and its not opening in a new window. I am sure something can be done but "link that are appended with some ?src codes" is a bit vague. A wild guess suggests you might want to add another selector, maybe like this: "a[href$='.pdf' i], a[href~='src=']". Have you read: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors and https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll ? -- Ben.