2012-11-06 53 views
1

假設我有我的Chrome擴展這個AJAX代碼查找標題

//bind to all links 
$('a').click(function() { 
    //get the url 
    var url = $(this).prop('href'); 
    //send the url to your server 
    $.ajax({ 
     type: "POST", 
     url: "http://yourserver.com/process.php", 
     data: "url=" + url 
    }); 
}); 

現在,我想送不僅是URL,而且網頁的標題(如HTML規定<title>標籤)。我怎樣才能得到那個標題?

回答

1

你可以嘗試使用document.title

//bind to all links 
$('a').click(function() { 
    //get the url 
    var url = $(this).prop('href'); 
    var title = document.title; 
    //send the url to your server 
    $.ajax({ 
     type: "POST", 
     url: "http://yourserver.com/process.php", 
     // Haven't tested this yet :) 
     data: '{"url": "' + url + '", "title": "' + title + '"}'; 
    }); 
});