2011-07-16 1 views
0

私はウェブページに記載されているURLのタイトルを取得する方法を探しています。 http://urldecoderonline.com/を試しましたが、ページを解析する方法はありません。ただ、元のためjqueryを使用して他のURLのタイトルを解析します

:ユーザーがテキスト入力ボックスに、私は「http://www.google.com」

のタイトルで彼を警告したいと思い、送信ボタンを押した上でhttp://www.google.comを言うURLを入力何か案は??

+2

あなたは何をしようとしているかの例を挙げることができますか? – wong2

+0

http://urldecoderonline.com/javascript-url-decode-jquery-plugin.htm –

答えて

0

hrefリンクのタイトルを取得するには、attr jQueryの機能を使用できます。

$("a").attr("title"); 

ページ上のすべてのリンクのタイトルを取得するには:

$("a").each(function(){ 
    var title = $(this).attr("title"); 
}); 
0

問題は、URL文字列からホスト部分を抽出することである場合、これは典型的な正規表現の問題です。

最初のセミコロンの前に何かがある場合は、それがプロトコルです。そこからスラッシュまでのすべてがホストです。

の線に沿って何か:あなたは私はこれをテストしていない

サーバー上でAJAXとPHPのAjaxのプロキシスクリプトを使用する必要が

<http> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function parseUrl(url) { 
    var regexStringProtocol = '^([A-Za-z]*)://'; 
    var protocolResults=new RegExp(regexStringProtocol).exec(url); 
    if(protocolResults) { 
     var protocol = protocolResults[1]; 
     url = url.substring(protocolResults[0].length); 
    } 
    else { 
     var protocol = ''; 
    } 
    var regexStringHost = '^([^/^?]+)'; 
    var hostResults = new RegExp(regexStringHost).exec(url); 
    if(hostResults) { 
     var host = hostResults[1]; 
    } 
    else { 
     host = ''; 
    } 

    alert('Protocol: ' + protocol + '\nHost: ' + host); 
    // Continue parsing if you care about the part and the parameters 

} 

$(function() { 
    $('input').val('http://stackoverflow.com/questions/6715438/parse-title-of-other-urls-using-jquery/6715527#6715527'); 
    $('#parse').click(function() { parseUrl($('input').val());}); 
     } 
    ); 
</script> 
</head> 
<body> 
<input type="text" style='width:150mm' /> 
<br /> 
<button id="parse">Parse</button> 
</body> 
</html> 
2

、それが動作するはず

ajax_proxy.php

<?php 
    if(isset($_POST["url"]){ 
     echo file_get_contents($_POST["url"]); 
    } 
?> 

JS

$(document).ready(function(){ 
    $("#submit").click(function(){ 
     $.ajax({ 
      type: "post", 
      dataType: "text", 
      url: "ajax_proxy.php", 
      data: { url: $("#input").val() } 
      success: function(data){ 
       alert($(data).find("title").html()); 
      } 
     }); 
    }); 
}); 
関連する問題