PDA

View Full Version : a real pain. getting the href of a clicked link



riptide
06-05-2007, 10:07 PM
okay I have part of this code from the forums. I have been helped before but i can't fix this part.

I tried to see if the href value of a clicked link was making it in to the variable(retry).
I've moved the alert around a bit but still I can't get to pop up right.

here
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if (el) { var retry=el.href}

okay then this

action.linksclass2=function(){
document.getElementById("misterioProgram").style.display="block";
document.embeds["newE"]["src"] =retry ;
document.getElementsByTagName("param")[0].value =retry; alert(retry);
if(el){alert(retry)}

return false;
}
var theas=document.getElementsByTagName('a');
for (var i_tem = 0; i_tem < theas.length; i_tem++)
theas[i_tem].onclick=action[theas[i_tem].className];

alert(retry)

I just can't find out what I have done wrong. I have alerts everywhere;still can't find out if the values are getting in tothe varable retry

Trinithis
06-05-2007, 10:58 PM
*Untested*



<html>
<body>
<a href="javascript:void(0)">anchor1</a>
<script type="text/javascript">
var o = document.getElementsByTagName("a")[0];
function clickHandler(e) {
window.retry = e.target?e.target.href:e.srcElement.href;
var retry = e.target?e.target.href:e.srcElement.href;
window.setTimeout("alert(window.retry)", 2000); //Yes, I'm using a string due to laziness
window.setTimeout("alert(retry)", 4000); //Should throw an error
}
(o.addEventListener)?o.addEventListener("click", clickHandler, false):o.attachEvent("onclick", clickHandler);
</script>
</body>
</html>

riptide
06-05-2007, 11:32 PM
okay cool but I can't chop up the full classnames object. It needs to be part of el.

um....thisis why. I've added stuff everywhere also.


var action=new Object();
action.linksclass1=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if (el) { var retry=el.href}

var linkValue = el.href;

if(el)
alert(el.innerHTML);
return false;
}

action.linksclass2=function(){
document.getElementById("misterioProgram").style.display="block";
document.embeds["newE"]["src"] =retry ;
document.getElementsByTagName("param")[0].value =retry; alert(retry);
if(el){alert(retry)}

return false;
}
var theas=document.getElementsByTagName('a');
for (var i_tem = 0; i_tem < theas.length; i_tem++)
theas[i_tem].onclick=action[theas[i_tem].className];

alert(retry)


take a look at this and see if it may be part of the problem.

function playSong(nItem,nSong){

document.Player.fileName = nSong;
document.getElementById('currTrack').lastChild.data = nItem.lastChild.data;
}
I was given that for some reason.

by the way if I need to send that link value in to an object tag in a Iframe. Is there any thing I need to add becides the Iframe name?

riptide
06-06-2007, 09:22 PM
Trinithis, you code does work. It throws an alert twice so

var retry = e.target?e.target.href:e.srcElement.href;
and
window.setTimeout("alert(retry)", 4000);
do work

The question is how to add this only to the links of the right class.


var action=new Object();
action.linksclass1=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;


if(el)
alert(el.innerHTML);
return false;
}

action.linksclass2=function(){
document.getElementById("misterioProgram").style.display="block";
var retry = e.target?e.target.href:e.srcElement.href;alert(retry);
document.embeds["newE"]["src"] =retry ;
document.getElementsByTagName("param")[0].value =retry;}

return false;
}
var theas=document.getElementsByTagName('a');
for (var i_tem = 0; i_tem < theas.length; i_tem++)
theas[i_tem].onclick=action[theas[i_tem].className];


does not work. but at least return false on the links works now

Trinithis
06-06-2007, 11:41 PM
Oh, I think it alerts twice because the "retry" has an implied "window" parent, and since "window.retry" exists, it thinks it is that.

Anyway,


var anchors = document.getElementsByTagName("a");
for(var i=0; i<anchors.length; ++i) {
if(anchors[i].className==="something") anchors[i].addEventListener("click", handler, false);
}

Trinithis
06-07-2007, 04:34 AM
I re-wrote your code. I can't test it because I don't have your html or anything, but I think it will work.


var action = new Object();
action.linksclass1 = function(e) {
e = window.event ? window.event : e;
var el = e.target? e.target : e.srcElement;
if(el) alert(el.innerHTML);
return false;
}
action.linksclass2 = function() {
e = window.event ? window.event : e;
var el = e.target? e.target : e.srcElement;
var retry = e.href;
alert(retry);
document.embeds["newE"].src = retry ;
document.getElementsByTagName("param")[0].value = retry;
document.getElementById("misterioProgram").style.display = "block";
return false;
}
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; ++i) {
anchor = anchors[i];
try {anchor.onclick = action[anchor.className];}
catch(e) {}
}

riptide
06-07-2007, 02:04 PM
this is part of the code. I changed the way things are set up thinking that some how having the variable in the wrong place was causing the alert to not show up. But thisis not the case. I tried your code but it still doesn't work.

I'm wondering why I can't just use el.href and call it from the methood...


var action=new Object();
action.linksclass1=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
var retry = e.target?e.target.href:e.srcElement.href;

if(el)
alert(el.innerHTML);
return false;
}

action.linksclass2=function(){
document.getElementById("misterioProgram").style.display="block";
alert(retry);
document.embeds["newE"]["src"] =retry ;

But take a look at this. Test this and what you get.

</head>
<body>
<a href="javascript:void(0)">anchor1</a>
<script type="text/javascript">
var o = document.getElementsByTagName("a")[0];
function clickHandler(e) {
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
var retry = el.href;
window.setTimeout("alert(window.retry)", 2000); //Yes, I'm using a string due to laziness
window.setTimeout("alert(retry)", 4000); //Should throw an error
}
(o.addEventListener)?o.addEventListener("click", clickHandler, false):o.attachEvent("onclick", clickHandler);
</script>

</body>
</html>

Trinithis
06-07-2007, 02:19 PM
You need "window." in front of "retry"


</head>
<body>
<a href="javascript:void(0)">anchor1</a>
<script type="text/javascript">
var o = document.getElementsByTagName("a")[0];
function clickHandler(e) {
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
window.retry = el.href;
window.setTimeout("alert(window.retry)", 2000);
}
(o.addEventListener)?o.addEventListener("click", clickHandler, false):o.attachEvent("onclick", clickHandler);
</script>

</body>
</html>

riptide
06-07-2007, 04:46 PM
okay...I really wish some of the best coders come take a look at this.

it's not working when added to my object;no alert. It's throwing the error: the return statement is out side of function.
document.embeds is newE is null or not an object

riptide
06-08-2007, 02:34 PM
hum......let me try something else. but it looks like I can't call it from the methods.

riptide
06-08-2007, 02:56 PM
nope, nope, test this. It look like the values can't be sent to the player either way. even if it still calls the alert.


<body>
<a href="http://www.fileden.com/files/2006/11/20/398972/Seal%20-%20Kiss%20From%20A%20Rose.mp3">anchor1</a>
<script type="text/javascript">
var o = document.getElementsByTagName("a")[0];
function clickHandler(e) {
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
window.retry = el.href;
window.setTimeout("alert(window.retry)", 2000);
}
(o.addEventListener)?o.addEventListener("click", clickHandler, false):o.attachEvent("onclick", clickHandler);

</script>
<object id="Player" width="300" height="450"
classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"
codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">
<param name="fileName" value="'+window.retry+'">
<param name="autoStart" value="true">
<param name="showControls" value="true">
<param name="loop" value="false">
<param name="uiMode" value="full">
<embed id="theE" type="application/x-mplayer2"
src="'+window.retry+'"
ShowControls="1"
width="235"
height="160">
</embed>
</object>
</body>


<body>
<a href="http://www.fileden.com/files/2006/11/20/398972/Seal%20-%20Kiss%20From%20A%20Rose.mp3">anchor1</a>
<script type="text/javascript">
var o = document.getElementsByTagName("a")[0];
function clickHandler(e) {
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
window.retry = el.href;
window.setTimeout("alert(window.retry)", 2000);
}
(o.addEventListener)?o.addEventListener("click", clickHandler, false):o.attachEvent("onclick", clickHandler);
document.embeds["newE"]["src"] =retry ;
document.getElementsByTagName("param")[0].value =retry;

</script>

<object id="Player" width="300" height="450"
classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"
codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">
<param name="fileName" value="">
<param name="autoStart" value="true">
<param name="showControls" value="true">
<param name="loop" value="false">
<param name="uiMode" value="full">
<embed id="theE" type="application/x-mplayer2"
src=""
ShowControls="1"
width="235"
height="160">
</embed>
</object>
</body>