开发者

AJAX: Getting the title of the loaded html document (jquery)

开发者 https://www.devze.com 2023-02-24 03:44 出处:网络
I am loading an html page within the body of 开发者_Python百科a landing page via jquery and ajax. I need the <title>Page Title</title> from the loaded document for the landing page.

I am loading an html page within the body of 开发者_Python百科a landing page via jquery and ajax. I need the <title>Page Title</title> from the loaded document for the landing page.

I have 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 have also tried a few other methods, but no luck. Any ideas?

Thank you!

EDIT: test.htm is a very simple document.

Example:

<html>
<head>
<title>Page Title</title>
<style>
....
</style>
</head>
<body>
....
</body>
</html>


As Pekka said, it does strip the head from the loaded document, so you'll have to parse it out of the raw text with a regular expression: (Let me know if this works)

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


Try to find for title after you have appended to main-load.

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


try to use this code:

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

Hope it works

0

精彩评论

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