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.
Yeah, I don’t really understand it either. 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
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)
Argh! :headbang: That’s the second time I’ve mistyped that.
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):
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.)