简体   繁体   English

无法在html中加载外部js文件

[英]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. 我有一个html页面,其中使用js绘制svg。 now I want to store this js in external file and call it (my html and js files are in same folder). 现在我想将此js存储在外部文件中并调用它(我的html和js文件位于同一文件夹中)。 <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'. <script src="show1.js"></script>这样做时,我的内部js无法找到在外部js中定义的函数,并给出“ ReferenceError:未定义DrawLine”。 I even tried using alert in external js to check whether it was being loaded or not, but even alert is not working. 我什至尝试在外部js中使用alert来检查它是否已被加载,但即使alert也不起作用。 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. 我发布此代码段,而不是显示内部和外部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: 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. 注意:这些页面是django项目的一部分,因此我的html和js存储在项目的template文件夹中。

you can use the static file app in django to serve external static files: https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/ 您可以在Django中使用静态文件应用来提供外部静态文件: 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),然后需要配置您的项目以在设置中提供静态文件

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. 在开发模式下,runserver命令将为您的静态文件提供服务。 you may want to serve the files directly from the web server in production mode. 您可能希望以生产模式直接从Web服务器提供文件。 also settings may vary depending on the django version, you can refer to the documentation of your version for further details. 此外,设置可能因Django版本而异,有关更多详细信息,请参阅您版本的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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