Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JavaScript be overused?

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 common--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.

like image 239
ledhed2222 Avatar asked Feb 10 '26 16:02

ledhed2222


2 Answers

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).

like image 97
Mchl Avatar answered Feb 13 '26 00:02

Mchl


Putting HTML in your JavaScript is a bad idea. If you want to keep common 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>Welcome to my home page!</h1>
       <p>Some text.</p>
     </body>
    </html> 

Java/JSP:

    <html>
     <body>
       <%@include file="header.jsp" %>
       <h1>Welcome to my home page!</h1>
       <p>Some text.</p>
     </body>
    </html> 
like image 45
jmort253 Avatar answered Feb 13 '26 02:02

jmort253