Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hyperlink Target = "_Blank"

Status
Not open for further replies.

JLizOB

Programmer
Dec 5, 2007
45
CA
When you set the target attribute of a hyperlink to "_Blank" or whatever you choose to make it open up in a new window, what are the chances of it being blocked by a popup blocker? I have a site with reference links to other sites and don't want people to leave my site when they click on these other links.
 
There is a reasonable chance it will be blocked.

However, more of an issue are the accessibility implications.
At the very least you should make people aware that the link will open in a new window before they click it.

Also, depending on the DOCTYPE you are working to, you may find the target attribute is deprecated.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Is there a better method to achieve my goal of the user clicking the link, but yet still retaining my site in their browser?
 
There isn't a better method since it's the action of keeping people on your site that is the problem.

However, you can use the following if you are using a doctype that doesn't allow the target attribute.

Code:
<a href="[URL unfurl="true"]http://www.someothersite.com"[/URL] onclick="window.open(this.href); return false;">Link</a>

This will open a new window, is valid under strict doctypes and will also keep the link working should javascript not work.

An extension of this idea might be to create a function like this:
This is rough and off the top of my head - it could be optimised and it should also probably be in the Javascript forum.

JavaScript:
function externalLinks() {
	if(!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName('a');
	for (var i=0; i<links.length; i++) {
		var thislink = links[i];
		if (thislink.getAttribute('rel')=='externalLink'){
			thislink.setAttribute('title','Opens in a new window');
			thislink.onclick = function(){
				window.open(this.href);
				return false;
			}
		}
	}	
}

The Javascript should run when the page is loaded. It will look for links with a rel attribute of "externalLink" and it will attach an event listener to any it finds. It will also add a title attribute to alert the visitor that the link goes to an external site.

Then you can simply attach a rel attribute to any links you wish to open in a new window

Code:
<a href="[URL unfurl="true"]http://www.someothersite.com"[/URL] rel="externalLink>Link</a>


NOTE: You may find that using Javascript may trigger a popup blocker however I've not really had a problem using the above technique.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
I think that removal of TARGEt attribute is a great mistake. There are still link that should not be opened in the same tab as the website, IMO. I think, if there would be a method, a diferent method for this type of links, that would be a solution.

The best solution IMO would be to have an elemnt what is directly used for open links in new tab/window so at browsers could notice them, and for example marking them with using a different cursor that warns that this will open in new tab/window.
 
HTML should not control behaviour. Hence the removal of target.

There are few, very few, instances where there is a necessity to open a page alongside another (in a new window). Besides anything else it's confusing for the user (though I understand how this argument could be reversed).

Opening a page in a new window more often than not breaks the flow of usage of a website. There's a back button in all browsers for a reason. New windows break the behaviour of these.

As designers we should try to avoid breaking the way a user's browser works to assist our own ends. It's akin to trespassing on someone elses land or intruding into their home.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Well said, Foamy! That may be why most browsers have a right-click menu option to open the link in a new tab - to give users control of what they see.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
I agree when it's for navigating to another page, but there are cases where a popup can be useful (for example, think of a popup calendar, or address finder etc). Granted, you could code the main page to show all of this inline, but it's sometimes easier to just have one page that you can pop open and pass some details back to the calling page.




-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Of course I know the reason target was removed. I remember some site, created by unprofessional, ordinarí people where all subpage opened in new window. It was very annoying for sure...

But now those who really need something to be opened in new window for some reason, now havo to work difficult and twisted solutions in so many lines, insted of 15 characters.

For small popups you can use AJAX for example - but this is still complicated than the "old way".
 
Mark & Stormheart

You can still create the behaviour you describe using either CSS or DOM Scripting to display/create a div. This wouldn't trigger any kind of blocker or navigate away from the page.

Using a technique similar to the way I described above you can make sure that the functionality would still be there even if Javascript was not availble.

In the same way we don't really need to use frames, we don't really need to create new instances of the browser (or a new browser tab). Of course there are always exceptions but I can't think of any right now that couldn't be done without opening a new window.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Yeah, that's what I meant by "code the main page to show all of this inline". It's just more work as you have to include it on every page that you want the functionality (unless you display it in an iframe when you display the hidden div).


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top