Jak Mogę uzyskać tytuł strony internetowej podanego adresu url (zewnętrznego adresu url) za pomocą JQuery / JS

Jestem nowicjuszem, więc przepraszam, jeśli to głupie pytanie ..

Więc starałem się uzyskać tytuł adresu URL za pomocą JQuery / JS. Nie chcę ładować zawartości adresu url, a następnie analizować tagi w nim.

Powiem jaśniej, mam zestaw adresów URL, powiedzmy 20, dla których chcę wyświetlić tytuły .. adresy URL, do których się odwołuję, nie są bieżącymi adresami URL, więc nie mogę użyć dokumentu js.tytuł ..

Więc chcę zrobić coś z formy SOMEFUNC.tytuł (URL) i zdobądź tytuł. Czy istnieje taka funkcja?

Author: user1014390, 2011-10-26

3 answers

Możesz również uzyskać tytuł dowolnej strony internetowej za pomocą tego API

Http://textance.herokuapp.com/title/

$.ajax({
      url: "http://textance.herokuapp.com/title/www.bbc.co.uk",
      complete: function(data) {
        alert(data.responseText);
      }
});
 12
Author: Durgesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-01 19:25:46

Coś takiego powinno działać:

$.ajax({
  url: externalUrl,
  async: true,
  success: function(data) {
    var matches = data.match(/<title>(.*?)<\/title>/);
    alert(matches[0]);
  }   
});

TheSuperTramp jest poprawny, powyżej nie będzie działać, jeśli externalUrl jest poza twoją domeną. Zamiast tego utwórz plik PHP get_external_content.php:

<?php
function file_get_contents_curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$url = $_REQUEST["url"];
$html = file_get_contents_curl($url);

preg_match('/<title>(.+)<\/title>/',$html,$matches);
$title = $matches[1];

echo  json_encode(array("url" => $url, "title" => $title));

Następnie w javascript:

function getTitle(externalUrl){
  var proxyurl = "http://localhost/get_external_content.php?url=" + externalUrl;
  $.ajax({
    url: proxyurl,
    async: true,
    success: function(response) {
      alert(response);
    },   
    error: function(e) {
      alert("error! " + e);
    }
  });
}
 18
Author: uncreative,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-20 21:35:01

Crossdomain request nie działa z Ajaxem, ale możesz napisać na swoim serwerze skrypt pobierający tytuł danej strony.

Jeśli używasz PHP, możesz użyć funkcji file_get_contents i preg_match, aby uzyskać tytuł. Ten facet już podał kod.

Http://www.cafewebmaster.com/php-get-page-title-function

Następnie w jQuery możesz dodać to do zdarzenia lub umieścić w funkcji.

//For the purpose of this example let's use google
var url = "http://www.google.com";

$.ajax({
  type: "POST",
  url: "./getURLTitle.php",
  data: "{url: \"" + url + "\"}",
  success: function(data) {
     //do stuff here with the result
     alert(data);
  }   
});
 2
Author: Eric B.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 12:26:07