0

I have a problem with linking in my table.

I use:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $(".clickable-row").click(function() {
            window.document.location = $(this).data("href");
        });
    });
</script>

And it works without flaws, but I can't make links open in new window using standard html code:

target="_blank"

in:

<tr class="clickable-row" data-href="http://www.google.com/">

doesn't work

Any ideas?

3
  • 1
    how hard is it to search google for open new window javascript or similar terms? Questions asked here should show at least some research effort was attempted Commented Jul 27, 2015 at 19:29
  • you code should be window.open($(this).data("href")); Commented Jul 27, 2015 at 19:30
  • Of course I know that. I need just some links to be opened in new window, not all of them. I do not post Q that are easy to solve with google... Commented Jul 27, 2015 at 19:35

4 Answers 4

3

You can do it like:

jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        if(this.hasAttribute("target")){
            window.open($(this).data("href"),$(this).data("target"));
        }
        else{
            window.document.location = $(this).data("href");
        }
    });
});

That checks if the <a> has a target attribute, and uses it if it does.

CodePen Demo

EDIT:
Here is the solution to your comment:

  jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        if(this.getAttribute("href").substr(this.getAttribute("href").length - 3)=== "###"){
            window.open(this.getAttribute("href").substring(0, this.getAttribute("href").length-3),"_blank");
        }
        else{
            window.document.location = $(this).data("href");
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

hmm, it works, but, I have another problem. Visual Composer "eats" target . Can you please rewrite this so that if the a href link contains ### in address, it opens in new tab? Example: www.google.com/### = new tab and www.google.com = same tab.
Or I can assign additional class.
Works like a charm. Thx!
Not a problem, glad I could help :D
1

window.open('href')

Window location sets your current window's location.

2 Comments

But that will open all the links in the new window. I just need it to accept target="_blank". I need to open just some links in new window. Not all of them.
@JackBauer I would use Jacob's answer then
0

What about using window.open="http://....","target="_blank")

SO Reference

Comments

0

I fixed the problem by adding two classes and two functions:

<script type="text/javascript">
    jQuery(document).ready(function($) {
    $(".istiprozor").click(function() {
        window.document.location = $(this).data("href");
    });
$(".noviprozor").click(function() {
       window.open($(this).data("href"),$(this).data("target"));
    });
});
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.