使用javascript提取页面标题 [英] Extracting page title using javascript

查看:86
本文介绍了使用javascript提取页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用网址提取外部网站的网页标题。

I'm trying to extract the page title of an external site by using a url.

您知道document.title如何返回网页标题JS正在运行?我想知道我是否可以说''http://google.com'.title(不起作用)或类似的东西来获取另一页的标题。

You know how "document.title" returns the title of the page the JS is running on? I was wondering if I could say "'http://google.com'.title" (doesn't work) or something similar to get the title of another page.

更新:
我做了一些搜索,显然这可以用JQuery完成。请参阅 http://www.google.com/search?q=jquery+提取物+网页+标题。如果我理解正确JQuery是javascript的扩展,它支持我正在做的事情。那么有人可以发布可以实现此目的的JQuery代码吗?

UPDATE: I did some searching and apparently this can be done with JQuery. see http://www.google.com/search?q=jquery+extract+page+title. and if I understand correctly JQuery is kind of an extension of javascript and its supported in what I'm doing. so can someone post the JQuery code that can accomplish this?

这是基本的想法:有一个输入框标有url。并且一个按钮标记为转换为页面标题,意图是输入框的值将更改为用户给出的网址标题(在他们点击按钮后)

this is the basic idea: there is an input box labled "url". And a button labled "Convert to page title" and the intention is that the value of the input box will change to the Title of the url given by the user (after they click on the button)

function getTitle(url){

    var title = [CODE HERE];
    return title;

}

谢谢!

推荐答案

我认为你在寻找$ .ajax,因为这是一个跨域请求,可以通过调用YQL从js完成:

I think you're looking for $.ajax, since this is a cross domain request, which can be done from js using a call to YQL:

http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide

编辑

好的,我终于得到了一个有效的例子:
http://jsfiddle.net/zXVcy/

Okay I finally got a working example: http://jsfiddle.net/zXVcy/

编辑

以下是修复xdomain.js中脚本标记问题的方法:

Here is how to fix the script tag problem in xdomain.js:

   if (_success) {
    var dataString = "";
    try { 
    dataString = data.results[0].replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '');
    }
    catch {
    dataString = data.results[0];

}
    // Fake XHR callback.
    _success.call(this, {
    responseText: dataString
    }, 'success');
       }
    }

这篇关于使用javascript提取页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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