获取执行 JavaScript 文件的 URL(主要是 IE6-7 问题) [英] Getting URL of executing JavaScript file (IE6-7 problem mostly)

查看:25
本文介绍了获取执行 JavaScript 文件的 URL(主要是 IE6-7 问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我一直在尝试组合一个通用函数来检索网页上正在执行的 JavaScript 文件的绝对 URL:

Hey all, I've been trying to throw together a generic function that retrieves the absolute URL of an executing JavaScript file on a web page:

http://gist.github.com/433486

基本上你可以这样调用:

Basically you get to call something like this:

getScriptName(function(url) {
    console.log(url);
    // http://www.example.com/myExternalJsFile.js
});

在页面上的外部 JavaScript 文件中,然后可以用它做一些事情(例如找到加载它的 <script> 标签).

inside an external JavaScript file on a page and can then do something with it (like find the <script> tag that loaded it for example).

它在我测试过的几乎所有浏览器(至少 Firefox、Chrome、Safari、Opera v10 和 IE 8)中都运行良好.

It works great in almost all the browsers I've tested (Firefox, Chrome, Safari, Opera v10 at least, and IE 8).

然而,它似乎在 IE 6 和 7 中失败.回调函数被执行,但检索到的名称是主 HTML 页面的 URL,而不是 JavaScript 文件.继续示例,getScriptName 使用参数调用回调:http://www.example.com/index.html

It seems to fail, however, in IE 6 and 7. The callback function gets executed, but the retrieved name is the URL to the main HTML page, not the JavaScript file. Continuing with the example, getScriptName invokes the callback with the parameter: http://www.example.com/index.html

所以我真正要问的是是否有其他方法可以获取当前 JavaScript 文件的 URL(可能是 IE 6 和 7 特定的hackery)?提前致谢!

So all I'm really asking is if there's some other way of getting the URL of the current JavaScript file (which could be IE 6 and 7 specific hackery)? Thanks in advance!

另外,这不会在所有情况下都有效,所以请不要推荐它:

Also, this won't work in every case, so please don't recommend it:

var scripts = document.getElementsByTagName("script");
return scripts[scripts.length-1].src;

我希望它在动态创建的脚本标签(可能没有放在页面的最后)的情况下工作,也就是延迟加载.

I'd like it to work in the case of dynamically created script tags (possibly not placed last in the page), aka lazy-loading.

推荐答案

抱歉,我怀疑您可能对此感到困惑.早于版本 8 的 IE 通常会从以下形式的 javascript 错误中给出错误消息:

Sorry, I suspect you might struggle with this. IE earlier than version 8 typically gives error messages from javascript errors of the form:

line: 342
char: 3
error: expected identifier, string or number
code: 0
url: http://example.com/path/to/resource

其中 url 是 window.location.href,而不是包含问题的外部 Javascript 资源的 URL.我建议 IE 提供无用的 URL 值,因为此时脚本 URL 对 IE 不可用,也不能用于您可能编写的任何 Javascript 来尝试显示它.

where the url is the window.location.href, rather than the URL of the external Javascript resource that contains the problem. I suggest that IE gives the unhelpful URL value since the script URL isn't available to IE at that point, and neither is it available to any Javascript you might write to try to display it.

我希望能够链接到 IE8 发行说明,其中说明此错误/功能已修复,因此我将其创建为社区 wiki.我的 MSDN foo 很弱!

I would love to be able to link to IE8 release notes which say this bug / feature has been fixed, hence the reason I created this as community wiki. My MSDN foo is pretty weak!

这篇关于获取执行 JavaScript 文件的 URL(主要是 IE6-7 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆