Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Array for ALL links, type set by variable

I use the following code to find all links in the DOM (Thanks, Morcalavin and hielo).

Now I want to be able to set a variable to determine if only local links should be added to the array, or if only external links should be added to the array, or if all links or no links should be added.

var linkTypes = 1 // 0 for no links, 1 for local links only, 2 for external links only, 3 for all links
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
      <title>Test</title>
      <script type="text/javascript">
      function report()
      {
          for( var i=0, limit=document.links.length; i < limit; ++i)
		{
			alert(document.links[i].href);
		}
      };
      window.onload=report;
      </script>
</head>
<body>
<div>
<a href="http://www.google.com">Link 1</a>
<a href="http://www.cnn.com">Link 2</a>
<a href="http://www.foo.com">Link 3</a>
<a href="http://loclahost/">Link 4</a>
<a href="http://irlazy.com/">Link 5</a>
<a href="http://slashdot.org">Link 6</a> 
</div>
</body>
</html>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>Now I want to be able to set a variable to determine if only local links should be added to the array
added to where? document.links is set by the browser automatically. Unless you want a copy of the links on another array:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
      <title>Test</title>
      <script type="text/javascript">
	 var linkType=1;
	 var linkList=new Array();
      function report()
      {
	 	linkList.length=0;
          for( var i=0, limit=document.links.length; i < limit; ++i)
            {
		  	if( linkType==1 && !/^http:\/\/localhost/i.test(document.links[i].href) )
               {
			   linkList.push(document.links[i].href);
			}
			else if( linkType==0 && /^http:\/\/localhost/i.test(document.links[i].href) ){
			linkList.push(document.links[i].href);
			}
            }
	return linkList;
      }
      window.onload=report;
      </script>
</head>
<body>
<div>
<a href="http://www.google.com">Link 1</a>
<a href="http://www.cnn.com">Link 2</a>
<a href="http://www.foo.com">Link 3</a>
<a href="http://localhost/">Link 4</a>
<a href="http://irlazy.com/">Link 5</a>
<a href="http://slashdot.org">Link 6</a> 
</div>
</body>
</html>

Open in new window

Avatar of hankknight

ASKER

Yes, I would like the links in a NEW array.

And this var should deturmine what links go into the array:
var linkTypes = 1 // 0 for no links, 1 for local links only, 2 for external links only, 3 for all links

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
      <title>Test</title>
<script type="text/javascript">
var linkType=1;
var linkList=new Array();
function report()
{
	linkList.length=0;
	switch(linkType){
	case 0:
		break;
	case 1:
		for( var i=0, limit=document.links.length; i < limit; ++i)
		{
			if( /^http:\/\/localhost/i.test(document.links[i].href) )
			{
				linkList.push(document.links[i].href);
			}
		}
		break;
 
	case 2:
		for( var i=0, limit=document.links.length; i < limit; ++i)
		{
			if( !/^http:\/\/localhost/i.test(document.links[i].href) )
			{
				linkList.push(document.links[i].href);
			}
		}
		break;
	case 3:
		linkList.push(document.links[i].href);
		break;
	}
return linkList;
}
      window.onload=report;
      </script>
</head>
<body>
<div>
<a href="http://www.google.com">Link 1</a>
<a href="http://www.cnn.com">Link 2</a>
<a href="http://www.foo.com">Link 3</a>
<a href="http://localhost/">Link 4</a>
<a href="http://irlazy.com/">Link 5</a>
<a href="http://slashdot.org">Link 6</a> 
</div>
</body>
</html>

Open in new window

Almost.  The problem is you hardcode the local domain:
        ^http:\/\/localhost

I don't want to have to hardcode the domain name for every website!

Could this be done just with JavaScript with something like:
         location.hostname
?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  Only one small thing:

case 3 give a JavaScript error.
Ooops, I missed the for clause:
function report()
{
        linkList.length=0;
        switch(linkType){
        case 0:
                break;
        case 1:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
 
        case 2:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( !re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
        case 3:
	   		for( var i=0, limit=document.links.length; i < limit; ++i){
               	linkList.push(document.links[i].href);
			}
                break;
        }
return linkList;
}

Open in new window

Here is the full modified code based on your last comment:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
      <title>Test</title>
<script type="text/javascript">
var linkType=3;
var linkList=new Array();
function report()
{
        linkList.length=0;
        switch(linkType){
        case 0:
                break;
        case 1:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
 
        case 2:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( !re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
        case 3:
	   		for( var i=0, limit=document.links.length; i < limit; ++i){
               	linkList.push(document.links[i].href);
			}
                break;
        }
return linkList;
}
 
 
      window.onload=report;
      </script>
</head>
<body>
<div>
<a href="http://www.google.com">Link 1</a>
<a href="http://www.cnn.com">Link 2</a>
<a href="http://www.foo.com">Link 3</a>
<a href="localhost.zz">Link 4</a>
<a href="http://irlazy.com/">Link 5</a>
<a href="http://slashdot.org">Link 6</a> 
<div onclick="alert(linkList)"; style="background: #ffeeee">Click Here to See Links</a>
</div>
</body>
</html>

Open in new window