1

I am working on a logic where I want user to be redirected to a particular page if JavaScript is not enabled. I have put a meta-tag which will refresh every few seconds and if JavaScript is enabled and I want to use JavaScript to remove that element.

I have tried many things but I have been unsuccessful in removing the tag. Also I tried to empty the content of the tag but it still redirects.

Is there any other way to deal with this issue?

I am posting some code for you to take a look, I just grabbed it from the web but seems to be emptying the contents when I debug through the code:

var m = $('meta');

for (var c = 0; c < m.length; c++) {
    m[c].parentNode.removeChild(m[c]);
    m[c].content = '';
}

I am also open to a server side solution but the client does not send much information such as if javaScript is enabled or not.......

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Lost
  • 12,007
  • 32
  • 121
  • 193
  • http://stackoverflow.com/questions/1554688/asp-net-redirect-if-javascript-is-not-enabled?rq=1 – adt Jun 15 '12 at 16:35
  • I saw the link and the questioner is trying to do the same exact thing(Removing Meta if javascript is enabled) But but he is also having hard time. – Lost Jun 15 '12 at 16:40

1 Answers1

6

Try this:

// THIS DOES NOT WORK
$( 'meta[http-equiv="refresh"]' ).remove();

It certainly depends on how soon your jQuery code executes and how soon your refresh triggers...

Update: The above method does not work. Even though the meta element is removed from the DOM, the browser still executes the refresh.

Live demo: http://www.ecmazing.com/misc/test-removing-meta-element/


I believe you should also be able to do this:

<noscript>
    <meta http-equiv="refresh" content="..."> 
</noscript>

So, if you wrap your meta element in a NOSCRIPT element, it should only be parsed if JavaScript is disabled.

Read about the NOSCRIPT element here: https://developer.mozilla.org/en/HTML/Element/noscript

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • $( 'meta[http-equiv="refresh"]' ).remove(); does not work. In fact, remove(); is completely unusable on metatages. I can see my Javascript go through $('meta').remove() code and still I can see meta-tags in my page source. Can you explain "` if you wrap your meta element in a NOSCRIPT element, it should only be parsed if JavaScript is disabled?`" – Lost Jun 15 '12 at 16:49
  • @Californicated The page-source is static - it's the text that the server sends to the browser, and it does not reflect the changes that you make by JavaScript. Instead you have to use the browse's dev tools to look at the (live) DOM tree. I am working on a demo... – Šime Vidas Jun 15 '12 at 16:53
  • @Californicated First, removing the META element from the DOM has no effect. The browser will still do the refresh. Second, the content of the NOSCRIPT element is only parsed (by the browser), if JavaScript is disabled (in the browser). – Šime Vidas Jun 15 '12 at 17:08