通过 JavaScript 或 jQuery 停止在 hashchange 事件上加载图像

发布于 2024-09-07 18:31:06 字数 815 浏览 8 评论 0原文

我正在使用 jQuery BBQ:后退按钮和查询库插件,用于创建一个页面,在单击链接时提取动态内容。单击链接时,哈希值会发生更改,并且会引入新内容(因此单击 href 的“默认”操作被禁用。)

该部分工作正常,但存在问题。

我的问题的示例


假设“主页”页面有一个 DIV,其中有许多图像和一个链接列表...

  • 第一页
  • 第二
  • 页第三页

图像可能需要一段时间才能加载,同时用户通常不会等待它们完全加载并单击“第一页”链接。

这将清除“主页”页面的内容并加载“第一页”内容。效果很好。

问题是,即使用户已从“主页”页面转移,“主页”页面中的图像仍在浏览器中加载。

我知道发生这种情况是因为该页面实际上并未加载已更改,我正在使用 BBQ 插件的 hashchange hack,但我想知道 JavaScript 中是否有一种方法可以告诉当前加载的所有图像停止 >hashchange 事件?

??示例代码就像...


$(window).bind('hashchange', function () {

    //code to stop images from loading

    // now load in new HTML content

});

I am using the jQuery BBQ: Back Button & Query Library plugin to create a page that pulls in dynamic content when a link is clicked. When the link is clicked the hash is changed and new content is pulled in (the 'default' action of clicking a href is therefore disabled.)

That part works just fine, but there is a problem.

Example of my problem


Say the "home" page has a DIV a number of images in it and a list of links ...

  • Page One
  • Page Two
  • Page Three

The images may take a while to load, in the meantime the user will often not wait for them to load fully and click the "Page One" link.

This clears out the contents of the "home" page and loads in "Page One" content. That works fine.

The problem is the images from the "home" page are still loading in the browser even though the user has moved on from the "home" page.

I know this is happening becuase the page hasn't actually changed and I'm using the BBQ Plugin's hashchange hack but I want to know if there is a way in JavaScript to tell all the images currently loading to stop on a hashchange event?

?? Example code would be like ...


$(window).bind('hashchange', function () {

    //code to stop images from loading

    // now load in new HTML content

});

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

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

发布评论

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

评论(2

暮年 2024-09-14 18:31:06

我在 WebKit (Chrome/Safari)、Firefox 和 IE 中测试了这个解决方案,它似乎有效。

对于大多数浏览器,我使用了一个使用 window.stop(); 的函数,如果浏览器不支持 window.stop(),则使用 IE 自己的方式执行此操作。

请在此处查看更多信息:https://developer.mozilla.org/en/DOM/window.stop

所以,对于浏览器支持它(Firefox、WebKit)的使用:

window.stop();

对于 IE 使用:

document.execCommand("Stop", false);

但是要小心...

这基本上就像按浏览器中的“停止”按钮一样,因此当前正在下载的所有内容都会停止,而不仅仅是停止图像(就像我想要的那样。)

所以,在里面......

$(window).bind('hashchange', function () {

});

这是我使用的功能......

function stopDownloads() {
    if (window.stop !== undefined) {
        window.stop();
    }
    else if (document.execCommand !== undefined) {
        document.execCommand("Stop", false);
    }   
}

I tested this solution in WebKit (Chrome/Safari), Firefox and IE and it seems to work.

I used a function that uses window.stop(); for most browsers, and IE's own way of doing it if the browser doesn't support window.stop()

See more here: https://developer.mozilla.org/en/DOM/window.stop

So, for browsers that support it (Firefox, WebKit) use:

window.stop();

For IE use:

document.execCommand("Stop", false);

However be careful ...

This is basically like pressing the "stop" button in the browser so everything currently being downloaded will stop, not just images (like I wanted.)

So, inside ...

$(window).bind('hashchange', function () {

});

Here is the function I used ...

function stopDownloads() {
    if (window.stop !== undefined) {
        window.stop();
    }
    else if (document.execCommand !== undefined) {
        document.execCommand("Stop", false);
    }   
}
一萌ing 2024-09-14 18:31:06

相当确信这是不可能的(但很高兴被证明是错误的)。

原因是您网站中嵌入的图像会导致 HTTP GET 请求发送到您的服务器。发送该请求后,您的服务器将处理并响应该请求,该请求将由浏览器处理。

现在,如果可能的话,我猜它会涉及到遍历每个图像并清空其 src 属性(我认为这不会起作用,因为,我说,请求已经发送了)。或者调用一些(可能依赖于浏览器的)机制来停止图像加载。

I'm fairly confident this isn't possible (but would be happy to be proved wrong).

The reason being is that images embedded in your website result in HTTP GET requests being sent to your server. Once that request is sent, your server is going to process and respond to that request, which will get handled by the browser.

Now, if it is possible, I would guess it would involve either going through each image and nulling out its src attribute (which I don't think will work because, as I said, the requests have already been sent). Either that or calling some (likely browser dependent) mechanism which halts image loading.

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