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 3969498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:58:15+00:00 2026-05-20T03:58:15+00:00

my js file loads on all pages. Some functions are meant to run only

  • 0

my js file loads on all pages. Some functions are meant to run only on certain pages like the homepage only http://site.com. Can javascript read the url of the page it’s being called from to determine if it’s the homepage?

  • 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-05-20T03:58:16+00:00Added an answer on May 20, 2026 at 3:58 am

    You can use the window.location object to get properties about the location. Some notable properties are:

    • window.location.href – returns the entire URL of the current page
      • example: http://www.google.com/subdir/subpage.html
    • window.location.hostname – returns just the hostname (the domain name, including any subdomains)
      • example: http://www.google.com
    • window.location.pathname – returns just the path (the part following the hostname/domain, but not including the query string (part of the URL that begins with a “?”) or the hash (part of the URL that begins with a “#”))
      • example: /subdir/subpage.html

    Although all this works well, I would recommend (like others have mentioned) that it would be better to do this server-side. The server can usually do stuff like this better than the client.

    Additionally, if you have all your JS code in a single central file, you could add something like this directly to the page (on the server) to trigger an event for just that page, which may be more reliable than JS location sniffing:

    <script type="text/javascript">
    // jQuery example
    $(document).ready(function () {
        // Run function that is specific for this page
        MyNamespace.initHome();
    });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main HTML file dynamically loads in content with; <html> <head> <script src=http://code.jquery.com/jquery-1.7.1.min.js></script> </head>
I want to change some string located in html file when it loads. For
In Python 2.7, when I load all data from a text file of 2.5GB
Trying to load a small .txt file into mysql but get all my data
I have a situation where my main SWF file loads many external SWF files.
I have a flash file which loads data from JSON and parses it. It
Ok so I have an swf file that loads into the webview from the
I have a index.php file which loads (require) 2 different files based on a
I'm exporting my php as jsonp and then calling a js file which loads
I have a matlab script, lets call it master.m, that loads a file called

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.