hankknight
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
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>
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
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>
ASKER
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
?
^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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Only one small thing:
case 3 give a JavaScript error.
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;
}
ASKER
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>
ASKER
I have posted a related question here:
https://www.experts-exchange.com/questions/23782371/Preload-page-in-iframe-from-dynamic-array.html
https://www.experts-exchange.com/questions/23782371/Preload-page-in-iframe-from-dynamic-array.html
added to where? document.links is set by the browser automatically. Unless you want a copy of the links on another array:
Open in new window