简体   繁体   English

JavaScript在本地和在线上的工作方式不同

[英]JavaScript working differently locally than online

I've noticed that some simple scripts in JavaScript that work perfectly on my local server don't work at all online. 我注意到有些JavaScript的简单脚本无法在本地服务器上正常运行,而这些脚本可以在本地服务器上完美运行。

How can be this possible? 这怎么可能呢? JS client-side scripting? JS客户端脚本? Any ideas? 有任何想法吗?

Sorry for not providing much information, and for my English, too. 抱歉,没有提供太多信息,我的英语也很抱歉。

Mine was a general question. 我的是一个普遍的问题。

For example: 例如:

$('#posticipa').click(function(){
    var stato = $('#future').css('display');
    if(stato == 'none'){$('#future').css('display', 'block');}
    else{ $('#future').css('display', 'none');}
});

This piece of code works perfectly on my local Apache server on Ubuntu 9.10, with Firefox 3.6, 3.5, Google chrome and Opera. 这段代码可以在我的本地Apache服务器上(Ubuntu 9.10),Firefox 3.6、3.5,Google chrome和Opera完美运行。

When I upload it on my remote CentOS server, also running Apache, it doesn't work. 当我将其上传到也运行Apache的远程CentOS服务器上时,它不起作用。 No errors are displayed in Firebug or the console; Firebug或控制台中不会显示任何错误; it just doesn't run. 它只是没有运行。

I'm using the same version of jQuery on both servers. 我在两台服务器上都使用相同版本的jQuery。

Check console in your browser for errors. 在浏览器中检查控制台是否有错误。 If it is Firefox - install firebug, if it chrome - press Ctrl + Alt + J. 如果是Firefox-安装firebug,如果是chrome-按Ctrl + Alt +J。

It depends on what your script is trying to do. 这取决于您的脚本尝试执行的操作。 "Any ideas?" “有任何想法吗?” is pretty broad. 非常广泛。 But client-side scripting has a lot more restrictions than server-side, for obvious security reasons. 但是出于明显的安全原因,客户端脚本比服务器端具有更多的限制。 For example, if you could access the client's file system through client-side JS, any website on the internet would be able to take control of your system. 例如,如果您可以通过客户端JS访问客户端的文件系统,则Internet上的任何网站都可以控制您的系统。

JavaScript Security Restrictions JavaScript安全限制

Having looked at your edited question, I think it's most likely that one of two things is happening: 看完您编辑过的问题后,我认为最有可能发生以下两种情况之一:

  • Some kind of error is causing the JS to fail before this code is hit (but you say FireBug isn't displaying an error, so if you've got FB set up right this shouldn't be the problem) 某种错误导致JS在命中此代码之前失败(但是您说FireBug没有显示错误,因此,如果您正确设置了FB,这应该不是问题)
  • The entire script is never getting hit, probably due to an incorrect src attribute in the script tag. 整个脚本永远不会被命中,可能是由于script标记中的src属性不正确。

Try putting a debugger; 尝试放置debugger; line or an alert somewhere very early in your JS code. 或在您的JS代码的早期某个地方出现alert That should tell you whether the script is getting hit (the debugger command should tell FireBug to go into debug mode, provided you have FB's Script tag enabled on when you load the page). 那应该告诉您脚本是否被命中( debugger命令应该告诉FireBug进入调试模式,前提是您在加载页面时启用了FB的Script标签)。

If it isn't getting hit, make sure the script's src is the correct, reachable URL. 如果没有成功,请确保脚本的src是正确的,可访问的URL。 If it is, move the line steadily down your code until it doesn't fire anymore. 如果是这样,请将该行稳定地移至您的代码下,直到不再触发为止。 That should help you figure out where the error is. 那应该可以帮助您找出错误所在。

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

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