I
I
Iskander Mammadov2015-02-10 16:19:56
JavaScript
Iskander Mammadov, 2015-02-10 16:19:56

How to pass static to js file (Django)?

There is a code in JS:

$('.prof-button').mouseenter(function(){
        $('.prof-button').attr('src','/themes/choosevuz/images/main-menu-buttons-profs-active.png')
});
$('.prof-button').mouseleave(function(){
        $('.prof-button').attr('src','/themes/choosevuz/images/main-menu-buttons-profs.png')
});

attr()How to pass the image URL as the second argument ?
Since all pictures are transmitted via (in HTML files, this is how it is)
{% static "расположение картинки в папке статики" %}

But this method doesn't work in JS file.
How can I still pass the Url of the image to JS?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Iskander Mammadov, 2015-02-10
@Gugr

The issue was solved trivially simply:
the second argument can simply be passed the path to the file, without static.

A
Alexander Sydorenko, 2015-02-10
@San40

The general approach is that before connecting the js file that displays the image, you insert the following html

<script>
    var pathToImage = '{% static "расположение картинки в папке статики" %}';
</script>
<script src="some-main.js"></script>

Whereas some-main.js will already operate on the pathToImage variable instead of a static path.

S
Stanislav Fateev, 2015-02-10
@svfat

The easiest way is to put the script in a Django html template and load it with
{% extends ... %}
{% block % ...}{% endblock %}
You can also load it through a template in a JavaScript variable {{ STATIC_URL }} and use it in other scripts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question