...

View Full Version : detect a href URL in page and assign function to it



deathmill
07-10-2005, 02:24 PM
hi, very odd one here and im not sure if it can be done and if so not sure where to start.

I have 2 links <a href="http://www.aaa.co.uk">a</a> and <a href="http://www.bbb.co.uk">b</a> for example.

without having the onlick function, is it possible to check the value of the a href for "aaa" and carry out a function like open newWindow, alert, anything and only do it for that type of URL, where as "bbb" would just function as normal. So its like a URL checker / validator changer.

cheers - Death = $Mill

jscheuer1
07-10-2005, 05:03 PM
<script type="text/javascript">
function updateLinks(){
links=document.getElementsByTagName('a');
for (i = 0; i < links.length; i++)
if (links[i].href.indexOf('aaa')!==-1)
links[i].onclick=function(){
window.open(this.href); // <<< Do whatever on the other side of the comment (//) marks
return false;
}
}
window.onload=updateLinks;
</script>

deathmill
07-10-2005, 08:01 PM
oh my gard! it can be done, thank you so so much, one things just popped into my head, is it possible to add some kind of sleep function, say for example delay the popup for 5 seconds or something once I click the aaa link.

Im so happy, your the man jscheuer1 ! :)

Death = $Mill



<script type="text/javascript">
function updateLinks(){
links=document.getElementsByTagName('a');
for (i = 0; i < links.length; i++)
if (links[i].href.indexOf('aaa')!==-1)
links[i].onclick=function(){
window.open(this.href); // <<< Do whatever on the other side of the comment (//) marks
return false;
}
}
window.onload=updateLinks;
</script>

jscheuer1
07-10-2005, 09:14 PM
Technically, yes. In practice, some browsers may see that as a pop up add and block it. All you can do is try and see what happens, use:

setTimeout("window.open(this.href)",5000);in place of:

window.open(this.href);

deathmill
07-10-2005, 09:25 PM
I'v been playing around with the setTimeOut function and I keep getting a url "undefined" error, doesn't look as if the "this.href" is picking up the URL from the a href when delayed using setTimeOut.

any other throughts. :thumbsup:

D

Mr J
07-10-2005, 10:26 PM
Is there a typo at this line


if (links[i].href.indexOf('aaa')!==-1)


should it not be

if (links[i].href.indexOf('aaa')!=-1)

As for your timeout try this


<script type="text/javascript">
<!--
function updateLinks(){
links=document.getElementsByTagName('a');
for (i = 0; i < links.length; i++){

if (links[i].href.indexOf('aaa')!=-1){

temp=links[i].href

links[i].onclick=function(){
setTimeout("window.open(temp)",5000); // <<< Do whatever on the other side of the comment (//) marks
return false;
}

}

}
}
window.onload=updateLinks;

//-->
</script>

jscheuer1
07-10-2005, 10:29 PM
Yeah, I hate when that happens, computers cannot do anything other than what we tell them and sometimes we have no idea what we are telling them. This worked in IE6 locally:

<html>
<head>
<title>Update Link - Demo</title>
<script type="text/javascript">
function updateLinks(){
var funcString
links=document.getElementsByTagName('a');
for (i = 0; i < links.length; i++)
if (links[i].href.indexOf('hoo')!==-1){
funcString='setTimeout("window.open(\''+links[i].href+'\')",5000);return false;'
links[i].onclick=new Function(funcString)
}
}
window.onload=updateLinks;
</script>
</head>
<body>
<a href="http://www.yahoo.com">Yahoo</a>
</body>
</html>

glenngv
07-11-2005, 07:14 AM
You can also use anonymous function in setTimeout/setInterval.

setTimeout(function(){window.open(this.href)}, 5000);

jscheuer1
07-11-2005, 11:30 AM
You can also use anonymous function in setTimeout/setInterval.

setTimeout(function(){window.open(this.href)}, 5000);Not in this case, as 'this.href' will not resolve as expected when the link is clicked.

glenngv
07-11-2005, 12:04 PM
You can put this.href in a local variable inside the onclick handler and then access that inside the setTimeout anonymous function.

<html>
<head>
<title>Update Link - Demo</title>
<script type="text/javascript">
function updateLinks(){
var url;
var links=document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++){
if (links[i].href.indexOf('.com')!=-1){
links[i].onclick=function(){
url = this.href;
setTimeout(function(){window.open(url)}, 5000);
return false;
}
}
}
}
window.onload=updateLinks;
</script>
</head>
<body>
<a href="http://www.yahoo.com">Yahoo</a>
<a href="http://www.google.com">Google</a>
</body>
</html>

jscheuer1
07-11-2005, 02:14 PM
Then the big question is: Is that better, worse or the same, for whatever reason(s), as putting the literal url into the function while writing it, as I did? Looks a little easier on the eyes than the escaped quotes mine required. It creates more variables at the same level of scope (assuming more than one link gets used), I think. Mine keeps using the same variable over until all the onclicks are written. I'm sure for something like this it is unimportant but, it's interesting to me as I go on coding other scripts.

glenngv
07-11-2005, 02:42 PM
For this situation, it might not make a big difference. But here are the advantages of using anonymous functions over the Function constructor and string expression in setTimeout/setInterval:

Easily maintained. If in the future you want to add more functionality other than opening a new window, you can easily modify it.
No confusing mixture of single and double quotes.
You can pass objects as parameters and not only strings/numbers.
The Function constructor and string expression in setTimeout/setInterval are slower than anonymous functions as they are similar to eval (http://www.codingforums.com/showthread.php?t=20143).

Of course, it has also disadvantages as using anonymous functions are prone to IE memory leaks (http://www.bazon.net/mishoo/articles.epl?art_id=824). Even my posted code seems to fall on that IE bug. :p Glad there is a fix for that. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum