Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: a real pain. getting the href of a clicked link

  1. #1
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default a real pain. getting the href of a clicked link

    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
    Code:
    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
    Code:
    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

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    *Untested*

    Code:
    <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>

  3. #3
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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.

    Code:
    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.
    Code:
    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?

  4. #4
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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.

    Code:
    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

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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,
    Code:
    var anchors = document.getElementsByTagName("a");
    for(var i=0; i<anchors.length; ++i) {
        if(anchors[i].className==="something") anchors[i].addEventListener("click", handler, false);
        }

  6. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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.
    Code:
    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) {}
    	}

  7. #7
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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...

    Code:
    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.
    Code:
    </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>

  8. #8
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You need "window." in front of "retry"
    HTML Code:
    </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>

  9. #9
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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: [s]the return statement is out side of function.[/s]
    document.embeds is newE is null or not an object

  10. #10
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    hum......let me try something else. but it looks like I can't call it from the methods.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •