Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31058

Re: Open all links in new window

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Open all links in new window
Date 2016-08-04 15:28 +0100
Organization A noiseless patient Spider
Message-ID <87wpjwsj4w.fsf@bsb.me.uk> (permalink)
References <f6247ab2-1ae8-4753-81ed-f5b2fcdf7944@googlegroups.com> <59e0b44d-8ea8-431d-ae12-e7585917f23f@googlegroups.com> <8737mku9we.fsf@bsb.me.uk> <7956cde6-c641-485f-9144-e7ce529f0cd8@googlegroups.com>

Show all headers | View raw


Chandramohan Akkoni <csakkoni@gmail.com> writes:

> On Thursday, August 4, 2016 at 3:35:28 PM UTC+5:30, Ben Bacarisse wrote:
>> Chandramohan Akkoni <csakkoni@gmail.com> 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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Open all links in new window csakkoni@gmail.com - 2016-07-27 01:44 -0700
  Re: Open all links in new window JJ <jj4public@vfemail.net> - 2016-07-27 17:40 +0700
  Re: Open all links in new window Flng Fck <nothanks@invalid.invalid> - 2016-07-27 15:21 +0200
    Re: Open all links in new window csakkoni@gmail.com - 2016-07-27 23:33 -0700
      Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-07-28 11:02 +0200
        Re: Open all links in new window wmgill <webworker1950@gmail.com> - 2016-08-14 14:23 -0400
          Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-14 23:31 +0200
            Re: Open all links in new window wmgill <webworker1950@gmail.com> - 2016-08-14 17:43 -0400
              Re: Open all links in new window Robert Baer <robertbaer@localnet.com> - 2016-08-18 16:11 -0800
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-19 11:52 +0200
                Re: Open all links in new window wmgill <webworker1950@gmail.com> - 2016-08-20 01:08 -0400
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-20 09:15 +0200
                Re: Open all links in new window wmgill <webworker1950@gmail.com> - 2016-08-20 12:09 -0400
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-20 21:50 +0200
                Re: Open all links in new window $Bill <news@todbe.com> - 2016-08-20 17:21 -0700
                Re: Open all links in new window "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-08-20 19:48 -0700
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-21 09:42 +0200
                Re: Open all links in new window Andrew Poulos <ap_prog@hotmail.com> - 2016-08-21 19:29 +1000
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-22 00:44 +0200
                Re: Open all links in new window Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-08-22 21:58 +0100
                Re: Open all links in new window $Bill <news@todbe.com> - 2016-08-22 21:52 -0700
                Re: Open all links in new window "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-08-23 12:23 +0200
          Re: Open all links in new window Scott Sauyet <scott@sauyet.com> - 2016-09-03 19:55 +0000
  Re: Open all links in new window Chandramohan Akkoni <csakkoni@gmail.com> - 2016-08-03 22:28 -0700
    Re: Open all links in new window Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-04 11:05 +0100
      Re: Open all links in new window Chandramohan Akkoni <csakkoni@gmail.com> - 2016-08-04 03:46 -0700
        Re: Open all links in new window Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-04 15:28 +0100
          Re: Open all links in new window Chandramohan Akkoni <csakkoni@gmail.com> - 2016-08-05 01:34 -0700
            Re: Open all links in new window Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-05 10:49 +0100
              Re: Open all links in new window Chandramohan Akkoni <csakkoni@gmail.com> - 2016-08-05 02:56 -0700
                Re: Open all links in new window Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-05 15:08 +0100
                Re: Open all links in new window Chandra <csakkoni@gmail.com> - 2016-08-05 09:44 -0700
                Re: Open all links in new window Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-05 21:26 +0100

csiph-web