magento javascript 与 jquery 不能很好地配合

发布于 2024-10-19 14:44:43 字数 2400 浏览 1 评论 0原文

当我的 Magento 网站加载 jquery 时,我在让自定义 JavaScript 文件在 Google Chrome 中正常运行时遇到了一些问题。这个问题似乎只出现在谷歌浏览器中。我已经在 Linux Ubuntu 10.10 (chrome 10.0.648.114 beta) 和 Windows XP (chrome 9.0.597.98) 中测试了该问题。 FF、Safari、IE好像没有这个问题。

当 jQuery 添加到要包含的 JS 文件中时,我个人的 JS 文件在发送硬刷新时不起作用(强制 js 文件的 200 请求)。如果我定期刷新(js 文件的 304 请求)并从缓存加载它,那么它就可以工作。

我已经在我的 magento 站点之外创建了一个虚拟页面,但当我包含来自 magento 站点的 JS 文件时,我仍然遇到这个问题。但是,如果我将 JS 文件移到 magento 站点之外,它可以正常工作吗?

我在我的 magento 管理中禁用了缓存,据我所知,没有任何模块或自定义代码/插件会影响要包含的 JS 文件。

这是我的虚拟页面的示例,该页面位于服务器的根目录中

gearlists.js 中唯一的内容是

alert('external');

所以我希望有两个警报弹出窗口当我加载该页面时。但在硬刷新时,我只收到一个弹出窗口。 “内部的”。

如果我转身并进行定期刷新,两个 JS 文件都会从浏览器缓存中加载,状态代码为 304,并且我会收到两个弹出窗口“外部”,然后是“内部”

不适用于硬刷新

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/jquery/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/ads/gearlists.js"></script>
        <script type="text/javascript">
            alert('Internal');
        </script>
    </head>
    <body>
    </body>
</html>

始终有效

在此示例中,如果我将 JS 文件从 magento 虚拟服务器移出并移至服务器的根目录中,则一切始终按预期工作。每次页面刷新时都会显示两个弹出窗口

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://localhost/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="http://localhost/gearlists.js"></script>
        <script type="text/javascript">
            alert('here');
        </script>
    </head>
    <body>
    </body>
</html>

现在,如果我从测试中删除jquery文件,我的自定义js文件总是工作正常,无论它是否在我的magento VS中提供...总是有两个弹出窗口。

我对可能造成这种情况的原因有点困惑。任何想法将不胜感激。

I'm having a bit of a problem getting my custom JavaScript file to play nice in Google chrome when jquery is being loaded in my Magento site. This problem only seems to be appearing in Google chrome. I've tested for the problem in both Linux Ubuntu 10.10 (chrome 10.0.648.114 beta) and Windows XP (chrome 9.0.597.98). FF, Safari, IE don't seem to have this problem.

When jQuery is added to the JS files to include, my personal JS file does not work when a hard refresh is sent (force a 200 request of the js file). if I do a regular refresh (304 request of the js file) and have it load from cache, then it works.

I have gone so far as to create a dummy page outside my magento site and I'm still getting this problem when I include the JS files from the magento site. but, if I move the JS files outside the magento site it works fine?

I have caching disabled in my magento admin and to my knowledge there isn't any module or custom code/plugins that would affect JS files that are to be included.

Here is an example of my dummy page that is sitting in the root directory of my server

The only thing gearlists.js has in it, is

alert('external');

so I would expect two alert popups when I load that page. but on hard refreshes, I'm only getting one popup. "Internal".

If i turn around and do a regular refresh, both JS files are loaded from the browser cache w/ a 304 status code and I get two popups "External" then "Internal"

Does Not Work With Hard Refresh

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/jquery/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/ads/gearlists.js"></script>
        <script type="text/javascript">
            alert('Internal');
        </script>
    </head>
    <body>
    </body>
</html>

Always Works

In this example, if I move the JS files out of the magento virtual server and into the root directory of my server, everything always works as expected. two popups are shown w/ every page refresh

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://localhost/jquery-1.5.1.js"></script>
        <script type="text/javascript" src="http://localhost/gearlists.js"></script>
        <script type="text/javascript">
            alert('here');
        </script>
    </head>
    <body>
    </body>
</html>

Now if I remove the jquery file from my test, my custom js file always works fine whether its served from withing my magento VS or not...There is always two popups.

I'm a bit stuck on what could be causing this. Any ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2024-10-26 14:44:43

好吧,我明白了。愚蠢的错误,但花了一些时间。文件的目录是网站名称 js/ads/gearlists.js 的首字母缩写

,结果是我的 adblock 扩展程序使用正则表达式来查找单词“ads”并过滤掉我的脚本。当我将目录从 ads 调整为 adsinc 时,问题就消失了。

这是有道理的,因为我的 chrome 扩展已同步,所以 id 在 Linux 或 Windows 中不起作用。

OK, I figured this out. dumb mistake, but it took some time. The directory for the files were the initials for the site name js/ads/gearlists.js

turns out my adblock extension uses a regex to find the word "ads" and filtered my script out. when I adjusted the directory from ads to adsinc problem went away.

makes sense that id didn't work in linux or windows since my chrome extensions are synced.

流绪微梦 2024-10-26 14:44:43

两个提示:

您可能需要在 Magento Connect 上安装 mxperts jquery 插件扩展 - 它是免费的,有助于调试人们放在您的 Magento 站点上的 jquery 脚本。

Google Chrome 没有已知的刷新缓存的方法 - 不过您可以重命名 js 文件,然后加载新文件。

Two tips:

You may want to install the mxperts jquery plugin extension on Magento Connect - it is free and helps debugging jquery scripts that people put on your Magento site.

Google Chrome has no known way of refreshing the cache - you can rename your js files though, then it loads new ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文