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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:03:17+00:00 2026-06-15T17:03:17+00:00

The base is dynamically set in php when an HTML page is built: $base_line

  • 0

The base is dynamically set in php when an HTML page is built:

$base_line = '<base href="' . $some_path . '/" /> ';
echo $base_line;

Now that I am in the HTML page I need to access this information ($some_path) and after searching for a few hours I don’t seem to find an answer. Please note that the loaded HTML page has a URL that is not related to the base and I don’t have access to the PHP code to modify it. The loaded page could have a URL like: http://xyz.com/index.php, but all other links in the page will be based on the value set by the base so I can’t get the base using the page URL.

I suppose I could grab one of the elements like an image and dissect it using DOM to find the base, but there should be an easier way to do this. Any ideas?

Using window.location doesn’t work in this situation as it will return what is related to the loaded page URL and not what has been set as base within it.

  • 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-15T17:03:18+00:00Added an answer on June 15, 2026 at 5:03 pm

    Update 3

    As @jorgecasar mentioned in his answer below, there is now a property, baseURI on every Node (which includes every Element).

    Calling document.baseURI will get you the base path to the page, taking into account the <base> tag.

    Note that it is a recent-ish property that is supported by all modern browsers, but if you are using older browsers you might either want to stick with the older answers or make sure you have a poly- or ponyfill for it (Babel’s standard polyfill seems to include one, though I couldn’t find specific documentation saying as much).

    Also, note that document.baseURI will give you a fully-qualified absolute path, whereas getting the href attribute from <base> will give you the exact value you provided, so there may be a slight difference in the usage of the two.


    Original

    If you want to get the value of the base element, you can do something like:

    var baseHref = document.getElementsByTagName('base')[0].href
    

    Or to be a little safer:

    var bases = document.getElementsByTagName('base');
    var baseHref = null;
    
    if (bases.length > 0) {
        baseHref = bases[0].href;
    }
    

    Update: a more concise way would be:

    const baseHref = (document.getElementsByTagName('base')[0] || {}).href;
    

    baseHref may be null if it doesn’t have a <base>.


    Update 2: Instead of using getElementsByTagName(), use querySelector():

    var base = (document.querySelector('base') || {}).href;
    console.log(base);
    <base href="http://www.google.com"/>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set the masterpage dynamically, But currently I have a base page
My problem is this I have a base page that creates content dynamically. There
I need to load a HTML wrapper dynamically depending on data that gets populated
Is it possible to dynamically set the view base type for a partial view?
I'm trying to dynamically create an iframe and set it's base tag before it
I set the href -Attribute of an <a ...> -Tag dynamically in a project.
my base class need to expose a method that for some derived classes would
I used ActiveRecord::Base.set_table_name to set my table name on a dynamically created ActiveRecord class.
My base class has properties that are used by a subclass. where should Release
My base controller class, BaseController , is inherited by public-facing controllers to access a

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.