It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Script to Populate <title> and <alt>

Re: Script to Populate <title> and <alt>

Postby rangana on Wed Mar 18, 2009 1:57 am

This should met your heart's desire:
Code: Select all
<script type="text/javascript">
/**
Script Created as part of forum request: http://www.devppl.com/forum/script-to-populate-title-and-alt-vt13075.html
Created by Raymond Angana a.k.a rangana in devppl.com/forum
Date Created: 3/17/09
Modification:
   * 3/18/09 - Get heading tags and set it to the title
   * 3/18/09 - Capitalize every word right after the whiste-space
This notice must stay intact for legal use
*/
window.addEventListener?window.addEventListener('load',function () {
   ray.customScript('h1','h2','h3'); // Finds first instance of these tags and change's the nodes to that of the title
   },false):
window.attachEvent('onload',function () {
   ray.customScript('h1','h2','h3'); // Finds first instance of these tags and change's the nodes to that of the title
   }); // FF : IE
   
var ray = {
   customScript:function () {
      var elArr = this.customScript.arguments;
      var imgs = document.getElementsByTagName('img');
      for ( var i = 0 ; i < elArr.length; i++ )
         if (document.getElementsByTagName(elArr[i])[0])
      this.setTitle(document.getElementsByTagName(elArr[i])[0].firstChild.nodeValue);
      for (var c = 0 ; c <imgs.length; c++)
         imgs[c].setAttribute('alt',this.clean(imgs[c].src));
   },
   
   setTitle:function (val) {
      document.title=val;
   },
   
   clean:function (img) {
      var imgs = img.split('/');
      return imgs[imgs.length-1].replace(/\.([a-z]){3}/gi,'').replace(/_/g,' ').replace(/\b[a-z]/g,this.cnvrt);
      
   },
   
   cnvrt:function() {
      return arguments[0].toUpperCase();
   }
}
</script>


Hope that helps.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Script to Populate <title> and <alt>

Postby sagreiss on Wed Mar 18, 2009 3:05 am

Yes, it's perfect. Thanks.

I did think of a way to test it for SEO.

I sent the FreeFind bot to spider my site, but the site map is returning Title for the page, instead of Test Heading.

Do you have any idea why it isn't using the dynamic <title>?

Do other bots, such as the GoogleBot, perform the same way? Is there any way to know?

(By the way, thanks again. If you ever need someone to correct the English on your CV or something, please let me know.)
sagreiss
 
Posts: 17
Joined: Mon Mar 16, 2009 6:49 am

Re: Script to Populate <title> and <alt>

Postby rangana on Wed Mar 18, 2009 3:12 am

There is no search engine (as per time being) that is able to read text nodes generated via Javascript.

So you'll be definitely out of lack on that.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Script to Populate <title> and <alt>

Postby sagreiss on Wed Mar 18, 2009 5:00 am

That's a little disappointing.

Is there some way the script could physically write the new <title> and <alt> to the file, so that machine could see it as well as man?
sagreiss
 
Posts: 17
Joined: Mon Mar 16, 2009 6:49 am

Re: Script to Populate <title> and <alt>

Postby rangana on Wed Mar 18, 2009 5:16 am

None as far as I know, but my wits isn't as good as someone else.

Someone might give you better insight, and apparently, that's not me.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Script to Populate <title> and <alt>

Postby sagreiss on Wed Mar 18, 2009 5:29 am

OK, I'll ask around and see if I can come up with anything. I'll send you back the modified code, if a solution is found. Thank you for your kind help.
sagreiss
 
Posts: 17
Joined: Mon Mar 16, 2009 6:49 am

Previous

Who is online

Users browsing this forum: No registered users and 6 guests