简体   繁体   English

Javascript 不能在 localhost 上运行,但它可以在 Live 站点上运行

[英]Javascript not working on localhost, but it is working on Live site

I am facing JavaScript problems on XAMPP Localhost.我在 XAMPP 本地主机上遇到 JavaScript 问题。 The Collapse Buttons, go to top button and jQuery news feed are not working on every site on my localhost.折叠按钮、转到顶部按钮和 jQuery 新闻提要在我的本地主机上的每个站点上都不起作用。

But Facebook, Twitter, Plusone buttons load correctly from remote server ie from the respective Social Sites.但 Facebook、Twitter、Plusone 按钮从远程服务器(即从各自的社交网站)正确加载。

So, I think it narrows me to the point that the JavaScript files hosted locally are not executed while those from Facebook etc. are getting executed.所以,我认为它缩小了我的范围,即本地托管的 JavaScript 文件没有被执行,而来自 Facebook 等的那些文件正在被执行。

I tried reinstalling XAMPP but didn't work.我尝试重新安装 XAMPP,但没有成功。

I know this is old but, I had the same problem and found this question while searching a solution.我知道这很旧,但是我遇到了同样的问题,并在搜索解决方案时发现了这个问题。 But I have lampp installed on ubuntu and since Linux asks permission to execute ANYTHING, the local js files which resided in htdocs were without permission.但是我在 ubuntu 上安装了lampp,并且由于 Linux 要求执行任何操作的权限,因此驻留在 htdocs 中的本地 js 文件未经许可。 After:后:

sudo chmod 777 -R /opt/lampp/htdocs   

... everything was OK. ...一切都很好。 Hope this helps future wanabee web developers.希望这对未来的 wanabee 网络开发人员有所帮助。

I know this post is old and the answer is already given, I just wanted to give an answer of a possible reason that I encountered in case someone else finds this and it does not help.我知道这篇文章很旧并且答案已经给出,我只是想给出我遇到的可能原因的答案,以防其他人发现它并且没有帮助。

Make sure your entire html code gets executed by viewing your page source.确保通过查看页面源代码来执行整个 html 代码。 With me, half my code was not being executed due to a PHP error that was working on localhost with the local database but not on live with the live database.对我来说,我的一半代码没有被执行,因为 PHP 错误在本地数据库上运行,但没有在实时数据库上运行。 This caused the HTML to cut of from that point above my javascript.这导致 HTML 从我的 javascript 上方的那一点开始剪切。

So check your page source.因此,请检查您的页面来源。 If it is being cut of somewhere, find out why :)如果它被某处切割,找出原因:)

It should not matter whether your files are local or not...javascript should still get executed.您的文件是否是本地文件应该无关紧要......仍然应该执行javascript。 Try using some debugging tool like to see if you get any errors that only occur on your local machine.尝试使用一些调试工具,例如查看是否有任何仅在本地计算机上发生的错误。 Because it could be that you are running some code that is depending on a domain or whatever.因为可能是您正在运行一些依赖于域或其他内容的代码。

I had the same problem.我有同样的问题。 I try a lot but finally solved.我尝试了很多,但最终解决了。 The main problem was my browser cache that prevents from loading the new JavaScript file.主要问题是我的浏览器缓存阻止加载新的 JavaScript 文件。 My recommendation is that clear your browser cache data then close the browser and reopen your site again it will work perfectly.我的建议是清除您的浏览器缓存数据,然后关闭浏览器并再次重新打开您的网站,它将完美运行。

remove any (pre in post ) blank spaces from filenames of your namespace example : localhost:port/dir /index.html从命名空间示例的文件名中删除任何(后置前)空格:localhost:port/dir /index.html
localhost:port/dir/index.html本地主机:端口/目录/index.html

In similar cases, If web project site runs JS on Live Server but not on locally opened index.html, try to check validity of HTML File Paths,在类似的情况下,如果网站项目站点在 Live Server 上运行 JS 而不是在本地打开的 index.html 上运行,请尝试检查 HTML 文件路径的有效性,

eg you may have written:例如,你可能写过:

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

instead of:代替:

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

Try to add ./ before file name.尝试在文件名前添加 ./。

./ means Current directory. ./ 表示当前目录。

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

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