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


Groups > comp.lang.javascript > #31055

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 11:05 +0100
Organization A noiseless patient Spider
Message-ID <8737mku9we.fsf@bsb.me.uk> (permalink)
References <f6247ab2-1ae8-4753-81ed-f5b2fcdf7944@googlegroups.com> <59e0b44d-8ea8-431d-ae12-e7585917f23f@googlegroups.com>

Show all headers | View raw


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.

-- 
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