hop38 hop38 - 6 months ago 39
HTML Question

Why won't my .html file connect to .js file?

I want to connect my

.html
to
.js
. I'm trying to run this simple program but it's not working. Below is the screenshot of my file path and files I'm working with.

enter image description here

Here's
map.html
:

<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>

</body>
<script type="javascript" src="map.js"></script>
</html>


Here's
map.js
:

document.write("testing");


The problem is below. How can I render the
.js
file along with the
.html
file?

Here's
views.py
:

def map(request):
return render(request, 'personal/map.html')

Answer Source

A common CMS convention is to save JavaScript files in a static folder. You save anything that you don't want your template engine messing with there: images, javascript, css, etc.

It looks like you may need to save map.js at this path:

mysite/personal/static/personal/js/map.js

After that, you'll need to update you script link in your HTML to something like:

<script src="static/js/map.js">

The src path here isn't relative to where you store the file on your computer, but to the URI that your web server associates with it. Depending on how you've set things up, you'll need some portion of the new path. Django has a few ways of linking to static resources, but I'm not familiar enough with the platform to tell you which option you should use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download