1

I have a website, in which when user clicks a link it opens up in the same window if it is of my website's page, else in new window if domain is different. But I am doing this manually like this:

<a href="http://www.wrangle.in" target="<%=checkdomain("http://wwww.wrangle.in")?"_parent":"_blank" %>">Open Link</a>

checkdomain() checks the domain name of the link and returns true if it's of my website else false. I used the code from [ HERE ] for this purpose.

My question is: Is there any efficient and client side way available for checking link domains and open up them in new windows/tab if of another website(domain)? Like a JavaScript solution will be better, but then again JavaScript can be disabled by user. So, is there any other solution? Even JS solution will be great. Ignoring the disabling by user.

Community
  • 1
  • 1
Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • It's often a bad idea to force links to open in a new window just for the sake of keeping someone on your site. It's unexpected behavior and users may not like that, and may be less inclined to use your site. If you're sure this is what you need, then go with a server side solution as it doesn't have the risk of JS being disabled. – mason Dec 18 '13 at 15:23
  • tell this to Facebook, Twitter, Google developers first. The reason is suppose user is doing some activity on our website, for example: writing an article, suppose he accidentally clicked the link directly. His article will be lost. Server side solution is currently what I am using. I wanted a way so that suppose if a page is added from outside by one of my developer, the solution can be applied to those pages too. Cause those pages might not be programmed to use server side solution. They may simply add the link without `target="_blank"`. – Aishwarya Shiva Dec 18 '13 at 15:28
  • 1
    Then it sounds like you don't have a programming issue, you have an issue with standardizing coding practices used by your development team. – mason Dec 18 '13 at 15:32
  • Also, if the user clicks a link on your site, regardless of whether the link is to an external site or not, their article would be lost unless you had some method for asynchronously saving their content as they type it. Your reason for wanting to force links to open in a new window isn't very logical. – mason Dec 18 '13 at 15:35
  • You can take it like this @msm8bball. I can't explain my whole idea its classified. But take it like this, I am creating a blogging website where HTML can be edited by the user. – Aishwarya Shiva Dec 18 '13 at 15:37
  • Also JS solution will be great for now if you have one? – Aishwarya Shiva Dec 18 '13 at 15:39
  • Regardless of what the user is doing, clicking a link to an internal site that opens in the same window would cause them to lose their work unless you save their work as they type it. So your reasoning for wanting to do it does not make sense, and saying "it's classified" doesn't motivate anyone to help you. It sounds like you have a server side solution, changing it to a client side solution presents no advantages. If you can't get your dev team to use the client side solution, why would they use server side? – mason Dec 18 '13 at 15:40
  • ok forgot internal site link. What if only external site link is taken under consideration? – Aishwarya Shiva Dec 18 '13 at 15:49

2 Answers2

1

Somewhere on the page, or in an external JS file:

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href")
            && anchor.getAttribute("rel")
            && anchor.getAttribute("rel").indexOf("external") >= 0)
            anchor.target = "_blank";
    }
}

window.onload = function() {
    externalLinks();
};

Then, any external links just need to have rel="external" in the markup. For example:

<a href="http://example.com" class="sample" rel="external">Click here</a>!

The main advantages of this approach is that you're not going to cause any validation errors, even with an XHTML Strict doctype. Users are also able to easily prevent links opening in new windows by simply disabling JS.

If you need the decision of external/internal to be made automatically (and client-side), you can alter the logic of externalLinks to base the decision on the href attribute rather than the rel attribute. Of course, if you've already got the external/internal logic functioning in your codebehind, I would recommend using that information to render the anchor with the appropriate semantics (with rel), rather than re-writing almost identical code in your client-side JS.

Brian S
  • 4,878
  • 4
  • 27
  • 46
  • If he is having trouble getting his team to add `target="_blank"` to external links, I don't see how getting them to add `rel="external"` is going to be any easier. So I think he needs it to be done automatically. – mason Dec 18 '13 at 17:10
0

Try comparing your link url's host part (www.wrangle.in) with following in you function logic.

string currentURL = HttpContext.Current.Request.Url.Host;

I do not recommend to compare the name (i.e http or https), you can split using substring function.

For Client side

var homeURL = document.location.hostname;
$('a').each(function() {

   if ( $(this+'[href*='+homeURL+']')) {
       $(this).attr('target','_self');
   }else{
       $(this).attr('target','_blank');
} });

This link may help you to understand Url Parts.

Aamir Waheed
  • 459
  • 4
  • 14