PDA

View Full Version : Direct javascript to work on only some links


maryb86
12-11-2006, 02:11 AM
Hi,

I have javascript that works on all links in an html. It is below:

<script type="text/javascript">
<!--
window.onload=function() {
var mywindow;
var w=656;
var h=454;
var l=10;
var t=10;
var features='width='+w+',height='+h+',left='+l+',top='+t;
var ar=document.getElementsByTagName('a');

for(c=0;c<ar.length;c++) {
ar[c].onclick=function() {
if(mywindow) {
mywindow.close();
}
mywindow=window.open(this.href,'',features);
mywindow.focus();
return false;
}
}
}
//-->
</script>

The code makes the link open in a new pop up window with a certain size. When you click on another link, it opens in that same pop up window and is on top.

This code used to be used in iframe html pages, so it worked fine. But now i'm converting everything to a different layout so I need the javascript to only work on certain links on the page. How can I direct this coding to only certain links? Is there some kind of name feature? I don't know anything about javascript!

maryb86
12-12-2006, 05:47 PM
Anyone? I'm sure it's really simple for someone fluent with javascript...

BonRouge
12-12-2006, 08:10 PM
I've added a few lines that will check the class name of the link. Add 'class="pop"' to any 'a' tags that you want to open in your pop-up window.
window.onload=function() {
var mywindow;
var w=656;
var h=454;
var l=10;
var t=10;
var features='width='+w+',height='+h+',left='+l+',top='+t;
var ar=document.getElementsByTagName('a');

for(c=0;c<ar.length;c++) {
var rE = new RegExp("(^|\\s)pop(\\s|$)");
if (rE.test(ar[c].className)) {
ar[c].onclick=function() {
if(mywindow) {
mywindow.close();
}
mywindow=window.open(this.href,'',features);
mywindow.focus();
return false;
}
}
}
}

maryb86
12-12-2006, 09:51 PM
Awesome! Thankyou :D