是否可以使用 jQuery 从标头动态删除元刷新标记?

发布于 2024-12-04 15:15:35 字数 152 浏览 2 评论 0原文

我在标题中有这样的元标记..

<meta http-equiv='refresh' content='0;url=http://stackoverflow.com/'>

是否可以使用 jQuery 动态删除它?

I have meta tag in Header like that..

<meta http-equiv='refresh' content='0;url=http://stackoverflow.com/'>

is it possible to Remove it Dynamically using jQuery ?

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

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

发布评论

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

评论(5

凤舞天涯 2024-12-11 15:15:35

为了在启用脚本支持时实现不同的行为,您应该在 标记之间包含元刷新,如下所示,

<noscript>
    <meta http-equiv='refresh' content='0;url=http://stackoverflow.com/'>
</noscript>

并在加载 DOM 后实现所需的功能。大致内容如下:

$(window).load(function() {
    // here
})

已确认正在使用最新的 Firefox 版本

In order to implement different behavior when scripting support is enabled you should include the meta refresh between <noscript> tags, like so

<noscript>
    <meta http-equiv='refresh' content='0;url=http://stackoverflow.com/'>
</noscript>

and implement the desired functionality after loading the DOM. Something along the lines of:

$(window).load(function() {
    // here
})

Confirmed working on the latest Firefox version

何以笙箫默 2024-12-11 15:15:35

不。

首先,加载 jQuery 库会花费太长时间,因此您必须直接使用 Javascript(如果有的话)来完成。

其次,即使元有一个 id 并且您将最简单的 JS 片段紧随其后:

<meta id="stopMe" http-equiv='refresh' content='0;url=http://stackoverflow.com/'>
<script>
    var meta = document.getElementById('stopMe');
    meta.parentNode.removeChild(meta);
</script>

仍然为时已晚,因为元中的 content=0 意味着立即执行刷新,因此脚本永远不会被执行。如果将脚本放在元之前,它将无法工作,因为还没有 DOM 元素可供引用。

No.

First, loading the jQuery library would take way too long so you'd have to do it with straight Javascript if anything.

Second, even if the meta had an id and you placed the simplest JS snippet immediately after it:

<meta id="stopMe" http-equiv='refresh' content='0;url=http://stackoverflow.com/'>
<script>
    var meta = document.getElementById('stopMe');
    meta.parentNode.removeChild(meta);
</script>

it would still be too late because the content=0 in the meta means to execute the refresh immediately so the script will never be executed. If you placed the script before the meta it wouldn't work because there would be no DOM element yet to reference.

坏尐絯℡ 2024-12-11 15:15:35

据我所知,这没有任何意义。您显示的标头应该会导致立即重定向,可能在任何 JavaScript 执行之前。

如果你可以使用 jQuery 来更新它,你不妨这样做:

location.href = "http://new.target";

我不知道这将如何在存在 Meta 标签的情况下执行 - 它是否总是击败 Meta 标签,总是输给它,或者导致不一致跨浏览器的结果。

也许可以告诉我们您的具体情况以及为什么需要这样做。

As far as I can see, this doesn't make any sense. The header you show is supposed to cause an immediate redirect, possibly before any JavaScript ever gets executed.

If you can use jQuery to update it, you might as well do this:

location.href = "http://new.target";

I don't know how this will be executed with the Meta tag present though - whether it will always beat the Meta tag, always lose against it, or cause inconsistent results across browsers.

Maybe tell us what exactly your situation is and why you need to do this.

后eg是否自 2024-12-11 15:15:35

试试这个:

$('meta[http-equiv="refresh"]').remove();

Try this:

$('meta[http-equiv="refresh"]').remove();
恬淡成诗 2024-12-11 15:15:35

对我有用的不是删除它,而是将值更改为一个非常大的数字,然后它永远不会刷新,如下所示:

$('meta').prop('content', '99999999');

What worked for me is not to remove it, but to change the value to a very big number, then it will never refresh as below:

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