How to add something right before the closing </head> tag using jQuery?

I tried this, but it does not seem to work:

$('</head>').appendTo("<meta http-equiv='refresh' content='0;url=http://google.com'>"); 
+4
source share
7 answers

Firstly, the element you are trying to add is not valid HTML, it is just a closing tag for the title and apparently already exists in the document. Secondly, you should not use appendTo in this case, but rather append :

 $('head').append("<meta http-equiv='refresh' content='0;url=http://google.com'>"); 

Thirdly, there is no reason to do this because you can just as easily change the location using javascript.

 window.location = "http://google.com"; 
+8
source

If you want it RIGHT to the end of the header: (as the question says)

  • Get title tag
  • Find the last item
  • Insert after this last item.

$ ("head"). find (": last"). after ("<meta stuff = 'stuff'>");

+2
source

What is your actual goal? Do you just want to send the user to Google?

 window.location = "http://www.google.com/"; 
+1
source

Unfortunately, append only works reliably inside the DOM.

0
source
 $('head').append("<meta http-equiv='refresh' content='0;url=http://google.com'>"); 
0
source

try the following:

  $ ('head'). append ("& LTmeta http-equiv = 'refresh' content = '0; url = http: //google.com'&GT");
0
source

Instead, you want to use this:

 $('head').append("<meta http-equiv='refresh' content='0;url=http://google.com'>"); 

Proper use for .appendTo() will be the opposite:

 $("<meta http-equiv='refresh' content='0;url=http://google.com'>").appendTo('head'); 
0
source

Source: https://habr.com/ru/post/1345646/


All Articles