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

html - Can JavaScript be overused? - Stack Overflow

programmeradmin4浏览0评论

I'm a "long time reader first time poster", glad to start participating in this forum.

My experience is with Java, Python, and several audio programming languages; I'm quite new to the big bad web technologies: HTML/CSS/JavaScript. I'm making two personal sites right now and am wondering if I'm relying on JavaScript too much.

I'm making a site where all pages have a bit of markup in mon--stuff like the nav bar and some sliced background images--so I thought I'd make a pageInit() function to insert the majority of the HTML for me. This way if I make a change later, I just change the script rather than all the pages. I figure if users are paranoid enough to have JavaScript turned off, I'll give them an alert or something. Is this bad practice? Can JavaScript be overused?

Thanks in advance.

I'm a "long time reader first time poster", glad to start participating in this forum.

My experience is with Java, Python, and several audio programming languages; I'm quite new to the big bad web technologies: HTML/CSS/JavaScript. I'm making two personal sites right now and am wondering if I'm relying on JavaScript too much.

I'm making a site where all pages have a bit of markup in mon--stuff like the nav bar and some sliced background images--so I thought I'd make a pageInit() function to insert the majority of the HTML for me. This way if I make a change later, I just change the script rather than all the pages. I figure if users are paranoid enough to have JavaScript turned off, I'll give them an alert or something. Is this bad practice? Can JavaScript be overused?

Thanks in advance.

Share Improve this question asked Jan 8, 2011 at 22:51 ledhed2222ledhed2222 7156 silver badges11 bronze badges 4
  • 2 You're just writing pure HTML and JS? You're not using a framework like PHP, ASP.NET, Ruby on Rails, Java, etc.? If not, you should be -- all those provide a much better vehicle for writing reusable chunks of HTML. – Kirk Woll Commented Jan 8, 2011 at 22:55
  • If your users use facebook they don't have JavaScript turned off (or many other popular websites). Normally businesses are more concerned about reaching an audience without JavaScript because they get much less traffic and every click counts (a potential customer). Can JavaScript be overused? Oh ya. But we often don't have much choice as far as reliance considering organizations like facebook are delivering their latest greatest APIs via JavaScript, jQuery provides as GREAT experience, etc. – John K Commented Jan 8, 2011 at 22:56
  • I'm not sure about overused, but this certainly strikes me as an inappropriate use of JavaScript. Building the core of HTML pages should be a server (rather than client) side activity. (SEO, users with JavaScript turned off, etc.) – John Parker Commented Jan 8, 2011 at 23:04
  • The alert() function also uses javascript, you can use a <noscript> tag though – qwertymk Commented Jan 8, 2011 at 23:20
Add a ment  | 

5 Answers 5

Reset to default 8

What you do is more of a job for server side scripting preprocessing in languages like PHP (or Python, or Java... actually there's server side JavaScript possible as well). This way you can deliver the content no matter if the browser supports JavaScript or not. Use JavaScript to do what server side technologies can't (image rotation, dynamic menus, AJAX calls etc).

Putting HTML in your JavaScript is a bad idea. If you want to keep mon HTML together, there are many different templating solutions out there for many different server side languages.

The simplest way to get started is to use php includes or jsp includes.

PHP:

    <html>
     <body>
       <?php include("header.php"); ?>
       <h1>Wele to my home page!</h1>
       <p>Some text.</p>
     </body>
    </html> 

Java/JSP:

    <html>
     <body>
       <%@include file="header.jsp" %>
       <h1>Wele to my home page!</h1>
       <p>Some text.</p>
     </body>
    </html> 

Its pretty bad. Consider Search Engine Optimization (SEO). Where do you get info for that pageInit()? AJAX call or just some static text?

JavaScript can be overused, as can any other piece of technology. Identifying when you're overusing it is tricky and subjective.

In this particular case, I'd remend the use of a server-side include technology to incorporate the header, footer, navigation, etc. This could be as simple as straight Apache SSI (for example), or if you were using Java, Python, PHP, etc. to construct the page, then making use of the idiomatic method for including other pages, views or templates into the current one.

This has the benefit that users with JavaScript disabled won't have issues with the content of your site, which is a fairly important aspect. Since there are perfectly good ways to solve this problem on the server side such that the client doesn't need to do any extra work, this is a case where I'd say JavaScript was a "bad idea".

If your javascript adds to the user experience then no. The web is moving forward and javascript is a technology that is significantly enhancing the user experience and turning the internet into a rich source of content and applications. It shouldn't be considered bad.

Ideally your site should work without javascript but as long as you are alerting your users to the fact that your site requires it then that's something at least.

If you're interested there is currently a free ebook by Jesse Skinner on Unobtrusive Ajax which talks about enhancing the user experience using ajax but without restricting those users who have javascript switched off. It's a good read.

Unobtrusive Ajax - Jesse Skinner

发布评论

评论列表(0)

  1. 暂无评论