Question

Good day. I am wondering is there any short script to apply the actual page title into the image title? So if I have this page title in my head section:

<head>
    <title>Dogs</title>
</head>

and these images:

<a class="sth" href="#" title="PROBLEM"><img src="img01.jpg" alt="sth"/></a>
<a class="sth" href="#" title="PROBLEM"><img src="img02.jpg" alt="sth"/></a>
<a class="sth" href="#" title="PROBLEM"><img src="img03.jpg" alt="sth"/></a>

I want to dynamically apply the title where it says "PROBLEM" from the page title. What is the easiest way of doing this? Does it need to be in php or can it be in javascript?

I just want "Dogs" to go in the place where "PROBLEM" is at the moment if you know what I mean. So probably "PROBLEM" needs to be replaced by a simple script. The document must remain a html by the way.

Was it helpful?

Solution

var title = document.title;
$('.sth').attr('title',title);

Edit, but I don't really see the point of doing this. The title of the site has little to do with the images, if it's added to all of them, what is the point anyways?

OTHER TIPS

in your head part use

<?php
$title_set = "Dogs";
echo "<title>$title_set</t?>itle>";
?>

Every where where you have images use this

<a class="sth" href="#" title="
<?php
echo $title_set;
?>
"><img src="img01.jpg" alt="sth"/></a>

Please remember to change $title_set for each page, or it will be set for every page.

Why couldn't you do this in PHP?

PHP

$title = "Dogs";

HTML

<a class="sth" href="#" title="<?php echo $title; ?>"><img src="img01.jpg" alt="sth"/></a>
<a class="sth" href="#" title="<?php echo $title; ?>"><img src="img02.jpg" alt="sth"/></a>
<a class="sth" href="#" title="<?php echo $title; ?>"><img src="img03.jpg" alt="sth"/></a>

So set a var in PHP, and echo it where you want it to be shown. PHP would be best so it renders as it should right from the start of the page load (Also so search engines can crawl it). You can do it via javascript, but this may cause dodgy results with SEO.

If you can, always do stuff like this in PHP (Or whatever server side language you code in), unless you absolutely have no other choice to use javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top