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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:20:12+00:00 2026-06-02T03:20:12+00:00

I am using a javascript file to do some button displays .When a user

  • 0

I am using a javascript file to do some button displays .When a user clicks on a button ,the current button’s image is replaced by another image (thus creating the effect that a new button appears in place of the old one) and vice versa.I created an <img> element with an id=’imgbutton’ and uses the elements attr('src',newimage) to display these buttons.

I was putting the javascript file in {{MEDIA_URL}}/js directory sothat the file can be added to template using

<script type="text/javascript"  src="{{MEDIA_URL}}/js/myscript.js" ></script>

In the javascript code(jquery 1.7.1),I am setting the image like this

..
var btnimg=$('#imgbutton');
btnimg.attr('src','{{MEDIA_URL}}/img/NewImage.png');

This causes a 404 error in javascript console..and a placeholder is shown instead of the image.
In the django templates ,the {{MEDIA_URL}}/img/OldImage.png is properly displayed..it is only in the javascript that the problem occurs..Can someone help me figure out why this happens?How do I correct this?

  • 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-02T03:20:13+00:00Added an answer on June 2, 2026 at 3:20 am

    Your problem is that the JS file is not processed by Django’s template engine. When I need to do this I normally put a small bit of JS directly in the the page to set variable values, and then that is referenced by the JS file that is loaded with the <link> tag.

    Update:

    Yes, you want to do something like this:

    <script>
    // first set the value you will be using.
    var MEDIA_URL = "{{MEDIA_URL}}"; // and anything else you need to set
    </script>
    
    <script type="text/javascript"  src="{{MEDIA_URL}}/js/myscript.js" ></script>
    

    Now you can reference that global value inside the JS file, e.g.:

    btnimg.attr('src',MEDIA_URL+'/img/NewImage.png');
    

    If there are a lot of these things, an Array or Object that holds all of them keeps your namespace tidy.

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

Sidebar

Related Questions

I am reading an xml file using javascript and then I need to submit
I am writing a XML file into a HTML file using JavaScript. Here is
When trying to dynamically load a Javascript file using jQuery I keep getting a
I am trying to parse a large XML file using JavaScript. Looking online, it
I am attempting to parse a XML file using Javascript and I'm running into
I'm posting a file in Javascript using the new FormData interface. When I send
I'm using the following regexp to validate numbers in my javascript file: var valid
is it possible to delete a file(s) inside the directory by just using javascript
What's the best practice for using ASP.NET expressions inside an external Javascript file? I'm
I'm working with a XAML file in a Silverlight Web application. I'm using 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.