Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:37:19+00:00 2026-06-05T14:37:19+00:00

I am building a website using PHP and JavaScript, and I feel that I

  • 0

I am building a website using PHP and JavaScript, and I feel that I have a good grasp on where to include my JavaScript, but a more specific situation has come up that has me confused. I currently have all of my JavaScript in one external file, which is being included on every PHP page.

Let’s say that I have a paragraph with an id=’myParagraph’ and I need to highlight this paragraph in red with JavaScript on page load. This paragraph is only on ONE PHP page and my website has about 50 different pages. I immediately assumed that I should throw some code into my one external JavaScript file, something like:

$('#myParagraph').css('color', 'red')

and the paragraph would be highlighted when that page loads.

My question is: is this the best way to do it? To my understanding, every time I load a page it will be searched for an element with the id myParagraph, yet 98% of my pages won’t even have that id. Is this wasteful? Should I instead include the following code:

function highlightParagraph()
{
    $('#myParagraph').css('color', 'red')
}

in my one JavaScript file and then put some inline JavaScript in the PHP file with the id myParagraph to call the function highlightParagraph() when it’s loaded? That way, only the one page with myParagraph will be searched and highlighted.

I feel like option 2 is the best, but I read all the time not to use inline JavaScript.

edit: I realize that for this example you would just use CSS. I’m just using it to get my question across

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:37:20+00:00Added an answer on June 5, 2026 at 2:37 pm

    You should have a one “big” js file with the infrastructure functions and all the pages should have a reference to it.

    Then each page should reference another js file with the functions related only.
    The good things about using external js files are:

    1. The files are cached after the first download => Faster surfing.
    2. Separate of concerns, you keep the presentation tier away from the scripting tier.

    Another important note:
    The best way to change css is with css… not javascript.
    I

    If you change the element style on DOM ready, just add the element definition

    #myParagraph{color: red;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a website using php and mysql.I have a login that is
I have been using PHP and JavaScript for building my dad's website. He wants
Building a website using a PHP based Ecommerce product Magento. The problem I have
I am building a website using php+mysql. I have a registration page and its
I have a website I am building using PHP and mysql and want to
I've have a web site I'm building using NodeJS, that needs to serve up
I'm building a mixed https/http website using PHP. When the user logs in (via
I'm going to build a website using xhtml, css, javascript and php. And this
Greetings, I'm building my personal website using php. My standard approach is to use
I'm building an IMDB.com like website using PHP/jQuery and a MVC approach (no OOP).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.