I can't use Javascript in Dreamweaver

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 14:45:23

问题


My Javascript wont activate on dreamweaver. I attached it and everything but when i try to call it with script tags it does not appear on my live preview. i have it attached by this code

<script src="file:///C:/Users/Matthew/Desktop/Untitled-2.js" type="text/script"></script>

If someone could please help that would be awesome! :D


回答1:


Live mode runs Your code in some virtual webserver and it cannot get local js file. Since browser may block resource from sharing (CORS). Think about putting js file to relative to html file and defining relative url to js file. Create js folder near to html file and put js file there and in Your html file define src="js/Untitled-2.js" – num8er 12 mins ago

Thanks Num8er




回答2:


In my opinion, it is best practice to keep all files relative to the project. This means setting up a project folder and keeping files organised in sub-folders.

Consider this project structure:

  • Project folder
    • CSS folder
      • style.css
    • Javascript folder
      • script.js
    • Images folder
      • image.jpg
    • index.html

The sub-folders are directly children of the project folder, and inside each folder is the corresponding files. The html file is also a direct child of the project folder (it's not in any other folder).

This means all the related files are relative to the html file.

So in your html file, you can link up these files easier.

<link href="CSS/style.css" rel="stylesheet">
<script src="Javascript/script.js" type="text/javascript"></script>
<img src="Images/image.jpg">

As you can see all the files are linked without a full path, this is called relative linking. Absolute linking is the opposite in which you specify the full path, such like you are doing at the moment:

<script src="file:///C:/Users/Matthew/Desktop/Untitled-2.js" type="text/script"></script>

This is good in certain places, however you should always try to aim for relative linking. If you follow this, you shouldn't have any more problems.



来源:https://stackoverflow.com/questions/48132491/i-cant-use-javascript-in-dreamweaver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!