Click to See Complete Forum and Search --> : Hyperlink Array in JavaScript?
Travman
10-08-2003, 05:10 PM
Would it be possible to load all of the hyperlinks on a given page in an array, then use Javascript to alter the target of those links to point them at the IFRAME of my choosing? Would this also allow control over the onclick handler in an HTML link?
Example. onclick="parent.location='URL';" in an standard hyperlink
There already is an array of all the links in the document, and you can reference it like this: document.links
So, if you want to change various things about them (such as the target and onclick hander) you'd use something like this:
<script type="text/javascript">
function init() {
for (i in document.links) {
document.links[i].target = "framename";
document.links[i].onclick = function() { /*some code*/ };
}
}
onload = init;
</script>
</head>
<body>
<a href="http://www.w3c.org">W3C</a>
<a href="http://www.webdevfaqs.com">Web Dev FAQs</a>But remember that this will fail for 13% of web users, as they have JavaScript disabled.
Travman
10-08-2003, 05:28 PM
That looks like it might do the trick.
Could I call this script from a page that contains an IFRAME and use it to alter the links that are present in the included page, even if the included page is of outside origin (not from my domain)?
I have one other question on this forum that is attempting to ask the ame question in a diferent way.
Thanks for the quick response!
Yes, you could call this script from a page that contains an iframe, and no, you could not use this script for a page that is not on your domain.