开发者

Is It possible to dynamically remove meta refresh tag from header using jQuery?

开发者 https://www.devze.com 2023-04-05 19:42 出处:网络
I have meta tag in Header like that.. <meta http-equiv=\'refresh\' content=\'0;url=http://stackoverflow.com/\'>

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 ?


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


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.


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.


Try this:

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


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消