1

I am quite new to javascript so probably this is not a very smart question - I'd be super thankful for your help

I have the following code:

$tp_baraja.find("li").click(function(){
        document.location=tp_questions[tp_pointer].selflink;
        return false;
});

Whereby the document link is provided from a database via php if that makes any sense.

Currently this opens in the same window but I want to force it to open in a new tab. How would I do that?

Amitabha Biswas
  • 3,281
  • 1
  • 11
  • 23
StnBnu
  • 75
  • 1
  • 11
  • 1
    It's better to add `a` inside of `li` and use `target="_blank"`. – dfsq Apr 06 '15 at 07:55
  • Your using a LI element, so this does not have a target attribute.. You can either use a inner anchor in each LI or have JS open the URL with a window.open... BUT i would suggest using a anchor as you might trigger a few spam/popup blockers by using window.open. – Angry 84 Apr 06 '15 at 08:14

3 Answers3

1

function openInNewTab(url) {
  $('<a>').attr('href', url).attr('target', '_blank')[0].click();
}
openInNewTab(tp_questions[tp_pointer].selflink);

Problem is most browsers have a default setting to prevent pop ups and this they consider pop up so you will see (at least in Chrome) message that a pop up was prevented

Marta
  • 388
  • 4
  • 9
0

You can try this

$tp_baraja.find("li").click(function(){
    window.open(tp_questions[tp_pointer].selflink, '_blank', 'width=600,height=400,scrollbars=yes,resizable=yes');
    return false;
});
Kiran Reddy
  • 556
  • 3
  • 11
0
$tp_baraja.find("li").click(function(){
    window.open(tp_questions[tp_pointer].selflink,'_blank');
    return false;
});   

Hope this will help you

tech-gayan
  • 1,373
  • 1
  • 10
  • 25