开发者

PHP/jQuery - getting html tag <title>xxx</title> content

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SOMETITLE</title>
</head>

<body>
<a href="http://www.facebook.com/sharer.php?u=<?php echo $url ?>&t=XXX">click</a&g开发者_JS百科t;
</body>
</html>

I want to use php/jQuery to get the content under <title></title> and place it to XXX at <a href="http://www.facebook.com/sharer.php?u=<?php echo $url ?>&t=XXX">, how to do it?

Updated 2

<?php

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

?>

<div id="language">
 <ul>
  <li class="last">
   <span><a id="block-header-facebook" href="http://www.facebook.com/sharer.php?u=<?php echo $url ?>&t=link"><img src="/home/images/icon/facebook.jpg" /></a></span><span><a href="#"><img src="/home/images/icon/twitter.jpg" /></a></span></li>
 </ul>
</div>

<script>
$('#block-header-facebook').attr('href').replace("link", "hi");
</script>

Thanks you


Use jQuery (just change the selector $("a") to be more specific, using an id or a class):

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SOMETITLE</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() { 
    $("a").each(function() { 
        $(this).attr("href", 
             $(this).attr("href").replace("XXX", $("html head title").text())
        ); 
    });
});
</script>
</head>

<body>
<a href="http://www.facebook.com/sharer.php?u=<?php echo $url ?>&t=XXX">click</a>
</body>
</html>

For the Update 2, change the script element:

<script type="text/javascript">
$('#block-header-facebook').each(function() {
    $(this).attr('href', $(this).attr('href').replace("link", document.title));
});
</script>

By the way I do too recommend using the Ralph's server approach (use this only if you need to do it on the client).

Added Recommendation (See Ralph comment)

Change the a-tag and script to:

<a id="block-header-facebook" href="http://www.facebook.com/sharer.php?u=<?php echo $url ?>">

<script type="text/javascript">
$('#block-header-facebook').each(function() {
    $(this).attr('href', $(this).attr('href') + "&amp;t=" + escape(document.title));
});
</script>

Less error-prone code, and I will work the same.


there is a simple function that you can use at this page

http://www.phpro.org/examples/Get-Text-Between-Tags.html


Or with JavaScript:

var link = document.getElementsByTagName("a")[0].href;
link = link.replace(/&t=.+$/, "&t=" + document.title);
document.getElementsByTagName("a")[0].href = link;


<?php
    $title = 'SOMETITLE';
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title ?></title>
</head>

<body>
<a href="http://www.facebook.com/sharer.php?u=<?php echo $url,'&t=',urlencode($title); ?>">click</a>
</body>
</html>

I think it's always preferable to do it server-side if you can. Less computation for the client, less JS dependency/cross-browser issues, and no "lag time".

Urlencoding isn't necessary in this example, but it will be if your title has spaces and other weird characters in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜