简体   繁体   中英

cant load external js file in html

I have tried all answers available but none can solve my problem. I have a html page in which I am drawing svg using js. now I want to store this js in external file and call it (my html and js files are in same folder). <script src="show1.js"></script> on doing so, my internal js cannot find functions defined in external js and gives 'ReferenceError: DrawLine is not defined'. I even tried using alert in external js to check whether it was being loaded or not, but even alert is not working. is there any settings that i will have to check? kindly help.

My code is really huge. I am posting this snippet instead that shows the internal and external js.

 <head>
<script>
//only variables are declared
<script>
</head>
<body>
<script src="show1.js"></script> 
<script>  drawline(10,10,20,20,'black',4);  </script>
</body>

show1.js:

function drawline(x1,y1,x2,y2,c,w)
{
//do stuff
}

Note:these pages are part of django project, and so my html and js are stored in the template folder of the project.

you can use the static file app in django to serve external static files: https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/

You can create a folder on you project to host your static assets (with the name static for example), then you need to configure your project to serve static files in the settings

STATIC_ROOT = 'path to your folder/static/'
STATIC_URL = '/static/'

then in your template:

   {% load staticfiles %}
    <head>
    <script>
    //only variables are declared
    <script>
    </head>
    <body>
    <script src="{% static 'show1.js' %}"></script> 
    <script>  drawline(10,10,20,20,'black',4);  </script>
    </body>

in development mode, the runserver command will serve your static files. you may want to serve the files directly from the web server in production mode. also settings may vary depending on the django version, you can refer to the documentation of your version for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM