最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Hrefs vs JavaScript onclick (with regard to Unobtrusive JavaScript) - Stack Overflow

programmeradmin6浏览0评论

What is best practice with regard to using links/<a> tags with explicit hrefs to other pages in your site (i.e. href="/blah/blah/blah.html) vs having hrefs/divs/etc. that don't have an explicit href and have their onclick set within the document ready handler with JavaScript in say a main.js file.

I'm not an expert when it es to web development, but I'm enjoying learning jQuery and such and find myself subscribing to the notion of Unobtrusive JavaScript. While both options above don't break the "don't have JavaScript within the HTML" part of that mentality, I suppose I'm hung up on the "Separation of structure and presentation from behavior". While it's admittedly more natural for me to put an <a> tag in there and explicitly set the href, I find myself thinking that this is really behavior and thus should be set within the JS.

Is that going to far, or am I just not used to it? The other side of me sees the benefit of putting it in the JS, b/c now I have the ability to pletely control the behavior of that link without having to change anything within the HTML. I guess you'd say I'm on the proverbial fence. Please help get me down. =)

(One note: The site uses JavaScript heavily, so the notion of providing functionality with JS turned off isn't really a concern as most of the site will not function without it.)

What is best practice with regard to using links/<a> tags with explicit hrefs to other pages in your site (i.e. href="/blah/blah/blah.html) vs having hrefs/divs/etc. that don't have an explicit href and have their onclick set within the document ready handler with JavaScript in say a main.js file.

I'm not an expert when it es to web development, but I'm enjoying learning jQuery and such and find myself subscribing to the notion of Unobtrusive JavaScript. While both options above don't break the "don't have JavaScript within the HTML" part of that mentality, I suppose I'm hung up on the "Separation of structure and presentation from behavior". While it's admittedly more natural for me to put an <a> tag in there and explicitly set the href, I find myself thinking that this is really behavior and thus should be set within the JS.

Is that going to far, or am I just not used to it? The other side of me sees the benefit of putting it in the JS, b/c now I have the ability to pletely control the behavior of that link without having to change anything within the HTML. I guess you'd say I'm on the proverbial fence. Please help get me down. =)

(One note: The site uses JavaScript heavily, so the notion of providing functionality with JS turned off isn't really a concern as most of the site will not function without it.)

Share Improve this question edited Sep 14, 2020 at 21:24 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 28, 2010 at 4:02 RC.RC. 28.3k10 gold badges79 silver badges94 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 10

That really is going too far for a multitude of reasons.

  1. It is mostly tricky code, which should be avoided.
  2. It provides no tangible benefit to your website.
  3. Doesn't have an eloquent fallback for no-js.
  4. It has negative effect on SEO. Specifically, bots wont run your script, and thus wont see the links and will ultimately not index your site properly.
  5. Probably most importantly, this effect can severely impact UX for screen readers or users with JS disabled (for instance, many mobile phone browsers disable JS)

In the end, unless you have explicit need to break the mold (e.g. legacy support) you should try your best to follow unobtrusive design, and this is very obtrusive in the sense that you are using JavaScript to create a static structure, something that is far better to be done with HTML.

Normal users won't really know the difference. However, search engines and SEO practices would require you to use the href="" to link to your other pages if you want the spiders to follow them. Same with if the visitor was using some screen reader or had some special accessibility needs. Many of those read the source code and not the DOM.

In general if you are linking to pages and actions use the href.

If you need to attach additional functionality or not really go to another page or action then use javascript onclick style, or use jQuery to attach events.

Links are not behavior — they represent links between one document and another. Web browsers offer the behavior of navigating to the linked page when you click on links, but this is the browser's behavior, and each browser has its own conventions for how best to do this — for example, primary click might open the page in the current tab, middle-click might open in a new tab, and M4 might open the link in a new page. Replacing this raw information with behavior breaks the browser's ability to offer this kind of choice.

And there are other clients that would be affected as well. Spiders and other bots will read your page for the information in your anchor tags to determine what the page is linked to. If you instead use "behavior," you're stripping this meaningful information from the page.

Agree with other posters. I'd add that if the href is static -- that is interactions on that page don't change it's value -- then you can consider it part of the page's "structure and presentation". In the unusual cases where it is dynamic and some action on the page changes its value -- that's when it bees "behavior" -- and only then would it be appropriate to let the JS handle it.

On the other hand though if the existing code already had a 9 dynamically set hrefs and you're just adding one static href, I'd probably follow the previous developer's lead for readability.

The jquery history plugin is pretty nice, it allows you to set href='#/url" this way you can have real urls, real back buttons, but your handers just listen for events bound to history.url.

http://tkyk.github./jquery-history-plugin/

Not using tags could probably also have some sort of usability problems for visually impaired users with screen readers.

发布评论

评论列表(0)

  1. 暂无评论