Hi I was wondering if anyone can help with this PHP problem.
Is it possible to use the text in a H2 tag and use that to populate the page title dynamically.
I'd also like to be able to use this same technique to add the H2 text into the meta description - Can anyone help?
If your h2 text is dynamically made then other parts of your web page can be dynamically created too. For example, if you have a variable $a = "My Text"
<?php
$a = "My Text"
?>
<html>
<head>
<title><?php echo $a;?></title>
</head>
<body>
<h2><?php echo $a;?></h2>
</body>
</html>
Using this technique, you can define the text of other parts of the web page.
That sounds like something that jQuery would excel at:
<script type='text/javascript' src='jquery-1.4-min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
document.title = $('h2:first').text();
});
</script>
To modify the meta data, you would do more of the same. I strongly recommend jQuery - Novice to Ninja as an amazing way to get into great depth with jQuery.
<html>
<head>
<meta description="" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('meta:first').attr('description', $('h2:first').text());
alert($('meta:first').attr('description'));
});
</script>
</head>
<body>
<h2>Testing 123</h2>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With