无法在“beforeunload”中设置background-image属性事件 [英] Unable to set background-image property in "beforeunload" event

查看:430
本文介绍了无法在“beforeunload”中设置background-image属性事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决IE的问题,不显示 .gif 图像时显示在 beforeunload 卸载事件。我进入这个答案,但发现它不工作,所以我用这个代码打了一点。



我有这样的东西:

  var 
spinner = $('。spinner-large'),
spinnerBackgroundImage = spinner.css('background-image');

console.log(spinnerBackgroundImage);
console.log($('。spinner-large')。css('background-image'));

spinner.css('background-image',spinnerBackgroundImage);

console.log(spinner.css('background-image'));

spinner.show();

这不工作。显示微调框,但没有背景图像。控制台包含:

  backend-extra.js:97 url(http://127.0.0.1/images/spinner-large .gif)
backend-extra.js:98 url(http://127.0.0.1/images/spinner-large.gif)
backend-extra.js:102无

我缺少什么?为什么我无法使用jQuery设置整个 background-image 属性值?



请注意, =http://stackoverflow.com/q/512054/1469208>不想在 background-image 属性中设置图像的路径,因为我已经知道我应该怎么做。我要求:为什么 background-image 属性值重置为 none

这里是 jsfiddle.net/trejder/sygyeqts/rel =nofollow> jsFiddle for above problem 。这似乎是100% beforeunload 事件相关的问题。当点击按钮时,非常相同的代码工作正常,而当刷新页面或导航到任何其他URL,控制台具有相同的输出像上面 - 属性是设置。

解决方案

网址周围应该有引号:

  background-image:url(http(...)spinner-large.gif); 

无效的格式化是足够的,它变成无。否则,创建一个jsfiddle,codepen,这样的东西可能是有帮助的。它似乎不是明显的行为。



编辑:这是超越我,但它似乎不是你的错。我建议工作。可能甚至在一个丑陋的方式,像没有动画的IE上的背景。 (优雅地退化应该还好吧!)


I'm trying to solve IE-problem of not animating a .gif image, when it is displayed in beforeunload or unload events. I step into this answer, but found it not working, so I played a little bit with this code.

I have come with something like that:

var
    spinner = $('.spinner-large'),
    spinnerBackgroundImage = spinner.css('background-image');

console.log(spinnerBackgroundImage);
console.log($('.spinner-large').css('background-image'));

spinner.css('background-image', spinnerBackgroundImage);

console.log(spinner.css('background-image'));

spinner.show();

This is not working. Spinner is displayed, but there is no background image. And console contains:

backend-extra.js:97     url(http://127.0.0.1/images/spinner-large.gif)
backend-extra.js:98     url(http://127.0.0.1/images/spinner-large.gif)
backend-extra.js:102    none

What am I missing? Why I can't set entire background-image property value with jQuery?

Note, that I don't want to set a path to image in background-image property, because I already know, how I should do this. I'm asking: Why background-image property value is reset to none, when I'm trying to change it with above method?

EDIT: Here is an jsFiddle for above problem. This seems to be 100% beforeunload event-related issue. When clicking button, the very same code works just fine, while when refreshing the page or navigating to any other URL, console have the same output like above -- property is not set.

解决方案

There should be quotes around the URL:

background-image: url("http(...)spinner-large.gif");

Invalid formatting is reason enough for it to turn into none. Otherwise it might be helpful to create a jsfiddle, codepen, something like that. It does not seem like obvious behavior.

Edit: this is beyond me, but it doesn't seem like your fault either. I'd suggest working around it. Possible even in an ugly way, like not animating the background on IE. (gracefully degrading should be okay anyway!)

这篇关于无法在“beforeunload”中设置background-image属性事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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