Why is attribute target=“_blank” causes an error when validating my html code? This is the way most tutorials in html in the internet is suggesting to open a link in a new window.
Thanks
Why is attribute target=“_blank” causes an error when validating my html code? This is the way most tutorials in html in the internet is suggesting to open a link in a new window.
Thanks
It’s not valid HTML any more. Since it’s a behavioural thing, you should use JavaScript for this.
You should also warn users about opening new windows, since it’s highly irritating if this happens without a warning. It also gives someone information if they think nothing happened (because their popup blocked blocked it).
Finally, you should really consider whether it’s really necessary. In some cases popups are useful, and in these cases telling the user about it is normally OK (I, for example, don’t mind). You might also consider using a lightbox instead of a popup.
I say F- validation… use what works. Every browser supports target=“_blank” and I’m not gonna stop using it until they don’t anymore. Why lose functionality for the 5% that don’t have JS enabled if you don’t have to?
Within ‘Transitional’ the target attribute is ‘valid’, Raffles meant it was deprecated or not available within HTML 4.01 ‘Strict’ for obvious reasons.
The HTML ‘_blank’ value renders the response in a new, unnamed window (or tab) and is usually concerned with frames are you wanting the results in a frame?
Albeit like was mentioned you should use unobtrusive progressive enhancement of script should you wish to perform such actions. Which lay outside the scope of a frameset, which may or may not be blocked anyway by the browser, i.e. both the opening of a new browser instance or the script option.
Misusing the target _blank solely for spawning a ‘popup’ style window can become an accessibility and usability issue since it breaks the back button, etc.
All big sites just use target blank. I have come to use it instead of a script. I have also come to not worry about stupid validation errors. This is one of those. If the powers that be are going to take away something they need to give a valid alternative aside from js.
For a start, I don’t believe that all big sites use target="_blank"
. Some do, but that doesn’t make it good practice.
If you’re using prehistoric bad-practice code like target="_blank"
then you should use a Transitional doctype, and then your page will validate. There’s no logic at all to stubbornly using a Strict doctype if you’re not prepared to stick to the standards required by that.
Validating Doctypes is a pedantic practice for people who care more about making code for the sake of the code than about making code for the sake of the people who will use it.
I never validate my sites’ HTML. Testing that they work in various browsers is all the validation I need.
Fine, if you want to flaunt your bad habits, go ahead. Do you test your site in all browsers? Including every new browser when it is released? No, you don’t. You can’t.
Validation doesn’t “guarantee” that you’ll get a perfect display and rendering in each and every browser, but if you do check you’ve got valid code, you’ve got a much smaller chance of it going wrong in some random browser that you didn’t test in.
Just remember that where a link opens IS SUPPOSED TO BE UNDER YOUR VISITOR’S CONTROL. When you don’t add a target they get a choice of three options - same tab, new tab, or new window - either predefined in their browser or by right clicking and choosing one. Adding a target removes one of those choices and makes your page LESS accessible for your visitor.
Of course people can always configure their browser to strip out the target attributes so as to fix your page for you (how kind of them) but not everyone knows how to fix web pages that their author has broken like that.
In my experience, validation, more often than not, just makes your life more difficult as a coder. Call it “flaunting bad habits” - I call it more efficient, more effective development. Validation does absolutely nothing for you… absolutely nothing… if things look and function the way you want, then what’s the point? Let’s take the target attribute… is there a single browser that doesn’t support the target attribute? No. And what % of browsers have Javascript disabled? 2%+. So I’m reaching 2% more users, and saving 15 seconds per instance for using the target attribute over Javascript. By what logic is that a “bad habit”?
I say forget the W3C and validation, and focus on building websites that work as well as possible. Sometimes that means going against W3C “rules”. I can give you examples of similar scenarios all day… for example… how do you center something vertically in a browser window? If I told you to use a table layout, you’d probably freak out, but the fact remains that it’s the simplest fastest way to do it, and will be accessible to a higher % of users, even if the W3C frowns on it… forget the W3C. Do what works (instead of “correct” nonsense that by some surreal “logic” doesn’t work as well as the “incorrect” way). You’ll be happier.
No - it makes your life a million times easier. Instead of having to check each page in several thousand different browsers and recheck each page in several dozen new browsers every day, you instead just make sure that the page is valid and works in the most popular six or seven browsers. There may still be a few hundred browsers where the page doesn’t work properly but at least you know that it is a problem with the browser rather than with your code.
If you don’t validate then the validation errors in your page are certain to stop the page working properly in many hundreds if not thousands of different browsers and while each browser may only have a negligible market share, all of them together will add up to a significant percentage of visitors who will be unable to use your page.
If validation made things harder then we wouldn’t bother with it - which would mean there’d be no point to standards either and so we could scrap that too and then everyone can do their own thing and create crap pages that only work for them and not for anyone else who might be using the same browser but who has it configured differently.
If you think validating makes things harder then obviously you need to go away and actually learn the language you are trying to validate properly first because if you know how to write HTML properly then you shouldn’t get many validation errors in the first place and fixing those you do get should usually be trivial. Trying to figure out how to get the various browsers to understand CRAP instead of HTML is far more difficult.
Just, wow. I can understand not feeling as if you must be bound by the validation requirements, but not to even test for validation … wow.
Similar to not being bound by clothing requirements and rules on where you are allowed to walk and equivalent to walking naked down the middle of a freeway right in the path of an oncoming truck which you expect to get out of your way and you intend to make trouble for the truck driver if he refuses to get out of your way.
<para>After all you are right and can do whatever you like completely disregarding any rules such as what constitutes valid HTML and everyone else is wrong and it is their <stupid>own fault<not-stupid> if their browser doesn’t understand your <smart>highly superior<unsmart> way of <slightly-bigger>marking up<end-sightly-bigger>your <must-read>highly important</may-read> content which of course all the search engines <lots-bigger>must<end-lots-bigger> display as the number one result for <must-read>all searches<may-read>.<im-done-with-this-para>
<lol>
I suppose <lol> would just produce a random lolcat. I can see a future for that in HTML5.
You guys make “valid” points, and I’m probably overstating my case for the sake of argument. I agree that you should validate to check for major HTML errors (e.g. missing closed tags, etc.), but where I disagree is worrying about making everything valid.
Here’s a case of a site I recently finished that has 2-3 validation errors that I have no intention to “correct” (because the “errors” work better than the “correct” alternative):
[Invalid] Markup Validation of http://www.strainbrain.com/ - W3C Markup Validator
I hope this explains where I’m coming from a little better.
Two things with that.
First - why stubbornly use a Strict doctype if you’re not going to abide by the rules for a Strict doctype? The target attribute is allowed under a Transitional doctype, so use that … then not only are you using the code that you want to, but it is valid HTML. Deliberately creating invalid HTML when you could make it valid with no loss of functionality is just perverse.
Second - forcing links to open in a new window by whatever method is bad practice. The majority of web users can open a link in a new tab/window if they so choose. Some browsers will open your links in a new tab and others in a new window - few users will know how to change their browser settings to get the one they want. If you use tabbed browsing, it’s frustrating and potentially disorientating to find a website spawning new windows. And what if people don’t want to open a new tab/window at all? Only the 1% of people enlightened enough to be using Opera even have that option. Let people browse the way they want to browse. Let them open new tabs/windows when they want to. The more you try to control their browsing behaviour, the more likely you are to frustrate them, and pissing people off is rarely a good business strategy.
For instance, I generally only open a new window when the link is going to another site. All links within my site open in the same window. This is pretty much how most do it.
Good point… but it’s not about stubbornness, more about indifference. I tend to prefer the transitional doctype, but I really just don’t care enough to look up the DTD and change it, so if I start with a strict template, I just go with that… again, with the belief that it really only means that I have 5 more minutes in my day and one less thing to worry about.
Second - forcing links to open in a new window by whatever method is bad practice.
I disagree. This link, for example, opens in a new window, and for good reason. It’s expected in the context of a forum that off-site resources linked to in a post will open in a new window. Any other behavior would result in a LESS usable forum. There are many similar such circumstances in which the user EXPECTS to be taken to a new site in a new window or tab. Whether W3C or Jakob Nielsen or anyone else says it’s good or bad doesn’t matter… all that matters is that the average user expects this behavior given certain criteria… thus it’s usable and good to do it.
Exactly. When the site your posting on (sitepoint) opens links in new windows is pretty hard to argue the point I would imagine.