Can any one help me on this HTML code

I’m afraid your question is not very clear. You use target=“_blank” on links, but it is very outdated and not good to use any more.

But is is used like so:

<a href="#" target="_blank">Contact</a>

You have a strict doctype, and tarter=“_blank” is not allowed with that doctype, so the page won’t validate.

Off Topic:

mmmm tarter sauce…

By the way, you have a strict doctype, so target=“_blank” also is not allowed with that doctype, so the page also won’t validate if you use that. :shifty:

[ot]

More tart than what? Or were you thinking of tartar sauce? ;)[/ot]

Off Topic:

mmm tartier… drool

You mean you want the new page to appear in a new window? ralph.m gave you an example of how to do that, but he also explained that it is no longer considered good practice. It means that people can’t get back to where they were using the back button, and it can be particularly difficult and annoying for those using screen readers and other assistive technologies.

Why do you want people to keep your home page open when they’re visiting another page? After all, most people can only read one page at a time. :slight_smile:

Although it’s now allowed in HTML5.

How dost one open the link in a new window now? Presumably with they power of CSS?

~TehYoyo

Usually with JavaScipt these days. E.g. if you are already using jQuery onthe site, you can add something like


$(document).ready(function(){
	$("a[@href^='http']").attr('target','_blank');
});

I suppose I’d understand that more if I actually knew jQuery…

Thanks Ralph.
~TehYoyo

Yeah, I don’t really understand it either. :slight_smile: But in this case, it tells the browser to add target=“_blank” to any link that includes “http” in its URL. So, for example, you could use this on a site where you wanted any link that includes “http” to open in a new window (such as an external link) but not to do so for internal site links (which might, for example, be root relative links, and thus not contain “http”).

If you just wanted all links to open in a new window, you could just use


$(document).ready(function(){
	$("a").attr('target','_blank');
});

All the same, as has already been discussed, this kind of behavior is not recommended now anyway, as it is confusing to users and kind of ruins the functionality of the web (the back button etc.).

If you are going to use tartet=“_blank”, it’s a good idea to use an outdated transitional doctype, to help remind you that the site is using outdated code. Except that …

Indeed, that’s the case—which is consistent with the HTML5 motto of “paving the cowpats” [typo intended :)].

Well, I really wouldn’t recommend using that syntax either…it probably won’t work. (:

In all seriousness, can you use jQuery to only open some links in new windows? For instance, I imagine internal page links and links to other parts of the site would want to be same-window links.

Also, why should we use jQuery to add attributes rather than just doing it ourself? Ease of use? (Universal w/out concious effort)

~TehYoyo

Argh! :headbang: That’s the second time I’ve mistyped that. :blush:

can you use jQuery to only open some links in new windows? For instance, I imagine internal page links and links to other parts of the site would want to be same-window links.

You can create any combination you like, but the example I first posted was just to open external links in a new window, with the idea that you don’t want people to leave your site to go to a new one. (I had a client once that insisted on this, despit my objections.)

Also, why should we use jQuery to add attributes rather than just doing it ourself? Ease of use?

Either just to get around the validator (if you have a strict doctype) or as an easy way to reverse engineer a site so that you don’t have to go around adding the attributes manually. But other than that, it is indeed pointless to use JS to do this.

Just to clarify … I did a site once that had many internal links (to other pages within the same site), such as

<a href="/contact/">contact us</a>

and lots of external links (to other sites) like

<a href="http://someothersite.com">bye bye</a>

Suddenly, the client demanded that all external links open in a new window (so that people wouldn’t leave his site when they clicked an external link).

Instead of going through the whole site and adding hundreds of target=“_blank” attributes to all those external links, I just added this simple code to the <head> of each page (which I did just once, because most of the <head> content was fed to each page via a single include file):

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
$(document).ready(function(){
	$("a[@href^='http']").attr('target','_blank');
});
</script>

That’s a much simpler solution, even though it means the user will have do download the entire jQuery library! (In the case of the site, that was already being used for other purposes anyway, so I’ve just added that first line above to show how you could do this if you weren’t already using jQuery on the site.)

The code is


http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
$(document).ready(function(){
	$("a[@href^='http']").attr('target','_blank');
});
</script>

… and where to put …

Put in <head>.

(I don’t know how Blogger works, though, so am not sure how much control you have over this stuff.)