Javascript 如何使用 JQuery/JS 获取给定 url(外部 url)的网页标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7901760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:00:03  来源:igfitidea点击:

How can I get the title of a webpage given the url (an external url) using JQuery/JS

javascriptjquerygettitle

提问by user1014390

I am a newbie, so excuse me if this is a silly question ..

我是新手,如果这是一个愚蠢的问题,请原谅我..

So what I was trying is to get the title of a URL using JQuery/JS. I dont want to load the content of the url and then parse tags in it.

所以我想要的是使用 JQuery/JS 获取 URL 的标题。我不想加载 url 的内容,然后解析其中的标签。

Let me be more clear, I have a set of urls, say 20 for which I want to display the titles .. the urls i am referring to are not the current urls, so I cant use js document.title ..

让我更清楚一点,我有一组网址,比如 20 个,我想显示标题..我指的网址不是当前的网址,所以我不能使用 js document.title ..

so i want to do something of the form SOMEFUNC.title(URL) and get its title. Is there any such function?

所以我想做一些类似 SOMEFUNC.title(URL) 的事情并得到它的标题。有没有这样的功能?

采纳答案by Durgesh

You can also get the title of any webpage using this API

您还可以使用此 API 获取任何网页的标题

http://textance.herokuapp.com/title/

http://textance.herokuapp.com/title/

$.ajax({
      url: "http://textance.herokuapp.com/title/www.bbc.co.uk",
      complete: function(data) {
        alert(data.responseText);
      }
});

回答by uncreative

Something like this should work:

这样的事情应该工作:

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

TheSuperTramp is correct, above will not work if externalUrl is outside of your domain. Instead create this php file get_external_content.php:

TheSuperTramp 是正确的,如果 externalUrl 在您的域之外,则上述内容将不起作用。而是创建这个 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));


then in javascript:

然后在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);
    }
  });
}

回答by Eric B.

Crossdomain request don't work with ajax, but what you could do is write a script on your server that fetch the title of a given site.

跨域请求不适用于 ajax,但您可以做的是在您的服务器上编写一个脚本来获取给定站点的标题。

If you are using PHP you could use the file_get_contents and preg_match function to get the title. This guy here already provide the code for it.

如果您使用 PHP,您可以使用 file_get_contents 和 preg_match 函数来获取标题。这里的这个人已经提供了它的代码。

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

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

Then in jQuery you could add this to an event or put it inside a function.

然后在 jQuery 中,您可以将其添加到事件中或将其放入函数中。

//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);
  }   
});