带有 URL 的大量 Javascript 会导致 Chrome(DevTools?)报告错误的 404

发布于 2024-12-09 04:46:00 字数 2175 浏览 0 评论 0原文

包含大量 javascript 的页面还包含在 javascript 中编码为 JS 字符串的 URL。

问题在于 Chrome(或者可能只是它的开发人员工具?)正在尝试导航到这些编码的 URL,就好像它们是 HTML 的一部分一样。

这种情况似乎只有在页面中有大量 javascript 时才会发生。

该页面看起来或多或少像:

<body>
    ....
    <script type="text/javascript">
        //<![CDATA[
        google.maps.event.clearListeners(window.kgmm["marker32"],"click");
        google.maps.event.addListener(window.kgmm["marker32"],"click",clicked);
        window.kgmp["marker54"]=new InfoBox({
            content: "<div class=\"wrapBox\" align=\"left\"><div class=\"imageBox\"><a href=\"#ir_rih\" target=\"_blank\"><span class=\"imageWrap\">"

/* the issue -> */  +"<img src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&amp;i=wp-content\/uploads\/2011\/07\/Bahrija &amp; Fomm ir-rih08.jpg&amp;w=115&amp;h=115\" border=\"0\" alt=\"\"\/><\/"

                    +"span><\/a><\/div><div class=\"specsBox\"><h2>Fomm Ir-Rih<\/h2><div class=\"shortDesc\"><\/div><a class=\"moreInfo\" href=\"#ir_rih\" onclick=\"return krtShowTabByHash(this);\">more details &raquo;<\/a><\/div><\/div>" ,
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-214, 0),
            zIndex: null
        });
        //]]>
    </script>
    ....
</body>

以下是控制台中的屏幕截图:

Screenshot


请注意,我并不是在寻找修复方法。请不要提出任何改进代码的建议。

我不是在寻找解决方案,而是在寻找为什么发生这种情况的答案。

注意: CDATA 不是应该阻止 chrome 将 javascript 内容解析为 XML 元素吗?


导致问题的示例网址

  1. 首先,请转到此处
  2. 打开控制台(右键单击 -> Inspect Element -> Console tab
  3. 单击页面列表中的第二项,名为“Mdina, Rabat, Dingli & Baħrija”
  4. 继续查看控制台页面加载时,您应该会看到上述错误。

注意:有人将问题标记为“过于本地化”。我想指出事实并非如此。我在多个网站上遇到过这个问题,但从未认真考虑过(直到现在),因为该网站仍然有效。

A page containing a good load of javascript also contains URLs encoded within the javascript as JS strings.

The problem is that Chrome (or perhaps just it's Developer Tools?) is trying to navigate to these encoded URLs as if they were a part of the HTML.

This only seems to happen when there is a lot of javascript in the page.

The page looks more or less like:

<body>
    ....
    <script type="text/javascript">
        //<![CDATA[
        google.maps.event.clearListeners(window.kgmm["marker32"],"click");
        google.maps.event.addListener(window.kgmm["marker32"],"click",clicked);
        window.kgmp["marker54"]=new InfoBox({
            content: "<div class=\"wrapBox\" align=\"left\"><div class=\"imageBox\"><a href=\"#ir_rih\" target=\"_blank\"><span class=\"imageWrap\">"

/* the issue -> */  +"<img src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content\/uploads\/2011\/07\/Bahrija & Fomm ir-rih08.jpg&w=115&h=115\" border=\"0\" alt=\"\"\/><\/"

                    +"span><\/a><\/div><div class=\"specsBox\"><h2>Fomm Ir-Rih<\/h2><div class=\"shortDesc\"><\/div><a class=\"moreInfo\" href=\"#ir_rih\" onclick=\"return krtShowTabByHash(this);\">more details »<\/a><\/div><\/div>" ,
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-214, 0),
            zIndex: null
        });
        //]]>
    </script>
    ....
</body>

The following is a screenshot on how it looks like in the console:

Screenshot


Be warned that I'm not looking for a fix. Please don't advise on any way to improve the code.

Rather than a fix, I'm looking for an answer as to why this is happening.

NB: Wasn't CDATA supposed to stop chrome from parsing the javascript contents as XML elements?


Sample URL causing the issue:

  1. First, go here.
  2. Open the console (Right Click -> Inspect Element -> Console tab)
  3. Click on the second item in the page list, named "Mdina, Rabat, Dingli & Baħrija"
  4. Keep looking at the console while the page loads, you should see the aforementioned errors.

NB: Someone has marked the question as "too localized". I'd like to point out that that is not true. I've encountered this issue on multiple websites,but never gave it much thought (until now) because the website still works.

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

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

发布评论

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

评论(1

我认为问题是您对 i url 参数的转义:

src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content\/uploads\/2011\/07\/Bahrija & Fomm ir-rih08.jpg&w=115&h=115\"

基本上,反斜杠转义对 URL 不起作用。如果您想访问该 URL,则需要将其发送为:

src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content%2Fuploads%2F2011%2F07%2FBahrija%20%26%20Fomm%20ir-rih08.jpg&w=115&h=115\"

Chris 编辑:将其解码为 javascript 字符串将产生:

src="?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content/uploads/2011/07/Bahrija & Fomm ir-rih08.jpg&w=115&h=115"

I'd put dollars to donuts that the problem is your escaping of the i url parameter:

src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content\/uploads\/2011\/07\/Bahrija & Fomm ir-rih08.jpg&w=115&h=115\"

Basically, backslash escaping does not work for URLs. If you wanted to go to that URL, you'd need to send it as:

src=\"?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content%2Fuploads%2F2011%2F07%2FBahrija%20%26%20Fomm%20ir-rih08.jpg&w=115&h=115\"

Chris Edit: Decoding it as a javascript string would yield:

src="?ajax=1&cls=owAjaxHandler&mtd=thumbnail&i=wp-content/uploads/2011/07/Bahrija & Fomm ir-rih08.jpg&w=115&h=115"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文