获取执行JavaScript文件的URL(主要是IE6-7问题)

发布于 2024-09-05 08:42:38 字数 954 浏览 8 评论 0原文

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

http://gist.github.com/433486

基本上你可以调用这样的东西:

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

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

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

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

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

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

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

我希望它能够在动态创建的脚本标记的情况下工作(可能不会放在最后)页),又名延迟加载。

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
});

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).

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

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

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!

EDIT: 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.

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

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

发布评论

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

评论(2

与他有关 2024-09-12 08:42:39

抱歉,我怀疑你可能会遇到这个问题。早于版本 8 的 IE 通常会给出以下形式的 javascript 错误的错误消息:

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 值,因为此时 IE 无法使用脚本 URL,并且您可能编写的任何 Javascript 也无法使用它来尝试显示它。

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

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

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.

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!

恰似旧人归 2024-09-12 08:42:38

这在很大程度上取决于您可以访问的内容。如果您试图完全在 JS 代码中完成此操作,如您所见,出于上面所示的某些原因,我不相信您能够做到这一点。也许你可以达到 90%,但不能确定。

如果您在 dotnet 环境中工作(这是我所知道的唯一环境),我建议使用一个模块来拦截所有 JS 请求并向其中添加请求位置或类似性质的内容。

我认为你需要从服务器端解决这个问题,而不是客户端。我认为您不会从客户端得到明确的答案。我认为您也将很难从服务器端获得答案,但您可能会更成功。

A lot of this depends on what you have access to. If, as it appears, you are trying to do this entirely within the JS code, I do not believe that you are able to do it, for some of the reasons shown above. You could get 90% of the way maybe, but not be definitive.

If you are working in a dotnet environment ( which is the only one I know ), I would suggest the use of a module that would intercept all JS requests and add into them the request location, or something of that nature.

I think you need to address this from the server side, not the client side. I do not think you will have a definitive answer form the client side. I think you will also struggle to get an answer from the server side, but you might be more successfull.

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