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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:56:01+00:00 2026-05-20T07:56:01+00:00

I have a javascript module which creates a div with a picture of a

  • 0

I have a javascript module which creates a div with a picture of a close button (“X”).
This div and javascript are placed in many places on my site.

Relative path solution: When a page includes the javascript, and the javascript uses a relative path for the image. The relative path is relative to the HTML-page. If HTML pages in different paths use this javascript I will need 2 different images.

Absolute path solution: I do not know what server my team-member is using for development (I know for sure that he is not developing on my server). This means that absolute paths will not work.

Is there a simple way of overcoming this problem? Like making the script somehow aware of its path?

  • 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-20T07:56:02+00:00Added an answer on May 20, 2026 at 7:56 am

    Mutable paths (test/staging/production domains) is always a problem in javascript, the best option is to include the root path of your application/website in the HTML. The obvious place to do this is in your template layer. For example:

    <body data-root="${rootContext}">
    <!-- or whatever syntax your template layer uses -->
    

    And grab it with javascript for usage in your scripts.

    var rootContext = document.body.getAttribute("data-root");
    

    Note, you can only do this when the DOM is ready (or when document.body is available, differs cross browser) 😉

    An alternative and in my view less pretty option is to simply render javascript.

    <script>
        var rootContext = ${rootContext} // or whatever syntax your template layer uses.
    </script>
    

    At least with the ‘data-root’ technique, you can store the value wherever you like and avoid a global definition.

    So in your code where you reference an image, you can do the following:

    img.src = rootContext + "/media/js/close.gif";
    

    Or create a nice helper method:

     // lets use a namespace to avoid globals.
     var myApp = {
         // still need to set this when DOM/body is ready
         rootContext: document.body.getAttribute("data-root"),
         getContext: function( src ) {
             return this.rootContext + src;
         }
     }
    
    img.src = myApp.getContext( "/media/js/close.gif" );
    

    In the helper method, you can also write some code to ensure proper uses of / and whatnot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the javascript module pattern, and have this so far: var APP; if(APP
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have Javascript function defined in the main index.html file. One part of this
I have javascript code embedded inside a html template file. When I load this
I have JavaScript code which copies the value of input file and paste it
I have a Javascript module the following Javascript: EntryController = function$entry(args) { MainView(); $('#target').click(function()
I have begun writing my 'class' type JavaScript functions like the Module Pattern or
On my Drupal site, I have made a Users page using the Views module,
I have a CMS with a form-module, which generates an awful markup, that makes
I have a javascript source code file which is about 32kb, and has javascript

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.