AJAX: getting the header of a loaded html document (jquery)

I load an html page into the body of the landing page through jquery and ajax. I need the <title>Page Title</title> from the uploaded document for the landing page.

I tried this but no luck:

 $.ajax({ url: "test.htm", cache: false, dataType: "html", success: function(html){ $('#main-load').html(html); $('#greeting').append($(html).find('title').text()); } }); 

I also tried several other methods, but no luck. Any ideas?

Thanks!

EDIT: test.htm is a very simple document.

Example:

 <html> <head> <title>Page Title</title> <style> .... </style> </head> <body> .... </body> </html> 
+4
source share
3 answers

As Pekka said, he takes his head off the downloaded document, so you have to parse it from the raw text using a regular expression: (let me know if this works)

 var title = html.match("<title>(.*?)</title>")[1]; 
+9
source

Try searching for title after adding to main-load .

$('#greeting').append($('#main-load').find('title').text());

+1
source

try using this code:

 $(html).attr("title"); 

Hope this works.

-2
source

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


All Articles