如何选择远程网页的标题 [英] How to pick the title of a remote webpage

查看:108
本文介绍了如何选择远程网页的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


如何使用javascript读取远程网页的标题?假设网页是:

  www.google.com 

我想读取该页面的标题;我应该怎么做? 解决方案

您无法单独使用jQuery获取此数据,但是您可以使用jQuery与PHP进行通信,或者其他服务器端语言,可以为您完成繁重的工作。例如,假设我们的服务器上有一个PHP脚本中包含以下内容:

 <?php#getTitle.php 

if($ _POST [url]){
$ doc = new DOMDocument();
@ $ doc-> loadHTML(file_get_contents($ _POST [url]));
$ xpt = new DOMXPath($ doc);
$ output = $ xpt-> query(// title) - > item(0) - > nodeValue;
} else {
$ output =网址未提供;
}

echo $ output;

?>

有了这个,我们可以有以下jQuery:
$ b $ ($pre> $。post(getTitle.php,{url:'http://example.com'},function(data){
alert(data) ;
});


Possible Duplicate:
how to get title of html page by javascript?

How I can read the title of a remote web page with javascript? Suppose the web page is:

www.google.com

I want to read the title of that page; how should I do this?

解决方案

You won't be able to get this data with jQuery alone, however you can use jQuery to communicate with PHP, or some other server-side language that can do the heavy lifting for you. For instance, suppose we have the following in a PHP script on our server:

<?php # getTitle.php

    if ( $_POST["url"] ) {
        $doc = new DOMDocument();
        @$doc->loadHTML( file_get_contents( $_POST["url"] ) );
        $xpt = new DOMXPath( $doc );
        $output = $xpt->query("//title")->item(0)->nodeValue;
    } else {
        $output = "URL not provided";
    }

    echo $output;

?>

With this, we could have the following jQuery:

$.post("getTitle.php", { url:'http://example.com' }, function( data ) {
    alert(data);
});

这篇关于如何选择远程网页的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆