# javascript function not accesible

**URL:** <https://forum.djangoproject.com/t/javascript-function-not-accesible/19504>\
**Category:** Templates & Frontend\
**Created:** [March 14, 2023, 1:21pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504 "2023-03-14T13:21:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![md1frejo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/md1frejo/32/12589_2.png) [@md1frejo](https://forum.djangoproject.com/u/md1frejo)\
**Post date:** [March 14, 2023, 1:21pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/1 "2023-03-14T13:21:47Z")

</div>

I have a javascript function in a file calles misc.js in my static dir in my django project:

kammardb/static/misc.js:

```auto
function df () {
    $('#cselect').click(function() { if($(this).prop('checked')==true) { $(':checkbox').prop('checked',true)}
				     else
				     { $(':checkbox').prop('checked',false)} } )
}

```

in my url I have the following code:

```auto
urlpatterns=[
  ...
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

```

and in settings:

`STATIC_URL='static/'`

However, the function is not accesible in my develop console when I load my page. What is wrong?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 1:27pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/2 "2023-03-14T13:27:25Z")

</div>

How are you including that in the page being loaded?  
(What do you have in your template to load that file?)

---

<div class="post-metadata">

**Author:** ![md1frejo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/md1frejo/32/12589_2.png) [@md1frejo](https://forum.djangoproject.com/u/md1frejo)\
**Post date:** [March 14, 2023, 3:26pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/3 "2023-03-14T15:26:18Z")

</div>

index.html:  
the last line is to check if the function df is accesible.

```auto
<head>
{% load static %} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    :
    :
<button class="button is-small is-light" id="cselect"> välj</button>
<script type="text/javascript" src="{% static 'js/misc.js' %}">df</script>
</head>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 4:03pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/4 "2023-03-14T16:03:46Z")

</div>

Standard things to check:

Look at your browser’s network tab in the developer tools to verify that a request was issued.

Look for the HTTP request in your server’s console to see if the request was made, and received a 200 and not a 404.

Use your browser’s developer tools console to see if the name for that function was defined.

Note that your attempt to put a reference to the function inside that script tag is usually not going to work, because that will frequently be executed _before_ the http request for the JavaScript file is executed and almost certainly before the response to that request is received and processed. You would need to write a script that can wait until all assets are loaded before running.

---

<div class="post-metadata">

**Author:** ![md1frejo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/md1frejo/32/12589_2.png) [@md1frejo](https://forum.djangoproject.com/u/md1frejo)\
**Post date:** [March 14, 2023, 5:35pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/5 "2023-03-14T17:35:20Z")

</div>

It is a 404, it can’t find it, so maybe I need to change my url.py

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 5:37pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/6 "2023-03-14T17:37:01Z")

</div>

What directory is that file in your project?

---

<div class="post-metadata">

**Author:** ![md1frejo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/md1frejo/32/12589_2.png) [@md1frejo](https://forum.djangoproject.com/u/md1frejo)\
**Post date:** [March 14, 2023, 5:44pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/7 "2023-03-14T17:44:55Z")

</div>

directory struture:

kammardb/templates/index.html  
kammardb/static/kammardb/misc.js

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 5:48pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/8 "2023-03-14T17:48:40Z")

</div>

But in your template, you are looking for it in:

> [@md1frejo](#):
>
> `'js/misc.js'`

which should probably be

> [@md1frejo](#):
>
> kammardb/misc.js

---

<div class="post-metadata">

**Author:** ![md1frejo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/md1frejo/32/12589_2.png) [@md1frejo](https://forum.djangoproject.com/u/md1frejo)\
**Post date:** [March 14, 2023, 5:58pm UTC](https://forum.djangoproject.com/t/javascript-function-not-accesible/19504/9 "2023-03-14T17:58:11Z")

</div>

ah, that is right. thanks a lot
