getElementById has no

Discussion in 'JavaScript' started by Marty_, Oct 7, 2008.

  1. #1
    This below function hides a tracking URL from the status bar, when the element id href link is clicked, however I'm having terrible trouble, trying to get the id to relate to the function, I'm assuming its because my id is inside another function, anybody have an suggestions or thoughts ?

    shortcut.add('1',function(){k2p.alert('<img src=\"http://aav.audio-adverts.com/ads/img/visit.png\" border=\"0\"> <a href=\"http://www.audio-adverts.com\"><b>NetAudioAds</b></a><br><small><em>Key2page Audio Advertisement.</em></small><br><br><b><a href=\"http://www.audio-adverts.com\" id=\"hidelink\" name=\"hidelink\" target=\"_blank\">Click here to visit Advertiser</a></b>');});
    
    window.onload=function hide() {
     document.getElementById("hidelink").onclick=function(e) {
    jx("http://aa.audio-adverts.com/www/delivery/ck.php?oaparams=2&&bannerid=19&&zoneid=0&&cb=78ce16eb2a&&maxdest="+escape("http://www.audio-adverts.com"));
     }
    }
    Code (markup):

     
    Marty_, Oct 7, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    i am not able to follow the idea you're having here...

    first off.. window.onload=function hide ...

    what calls hide? wouldnt that just define it?

    second:
    document.getElementById("hidelink").onclick=function(e) {
    jx("http://aa.audio-adverts.com/www/delivery/ck.php?oaparams=2&&bannerid=19&&zoneid=0&&cb=78ce16eb2a&&maxdest="+escape("http://www.audio-adverts.com"));
    }
    Code (markup):
    the 'e' here relates to the click event (which you need to stop, ideally) but within the scope of the function, this relates to the element itself (hidelink).

    i assume jx will redirect to the real url for you.

    in your function that defines the hidelink, you have an argument '1' - if that's a unique id you need to use, why not:
    warning - pseudo /partial code, not full - don't copy and paste :D (looking at the way you code, probably a moot warning but still)
    
    // take that anonymous function outside and define as:
    var setAlert = function(what) {
    ...<a href=\"http://www.audio-adverts.com\" id=\"hidelink\" name=\"hidelink\" target=\"_blank\" data-bannerid='"+what+"' ... more params as needed, html5 compliant storage within the element itself.
    }
    
    shortcut.add('1', setAlert('1'));
    // then...
    // on within your click function you can also do:
    var bannerid = this.getAttribute("data-bannerid"); // returns back your id.
    jx("http://aa.audio-adverts.com/www/delivery/ck.php?oaparams=2&&bannerid="+bannerid+"&zoneid=0&&cb=78ce16eb2a&&maxdest="+escape("http://www.audio-adverts.com"));
    
    Code (markup):
    i hope i am not missing the point here :)
     
    dimitar christoff, Oct 8, 2008 IP
  3. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are correct, I posted the wrong code, sorry to have wasted your time: This is the original code, perhaps this will shed some light on it as to what I'm trying to achieve. Which is to hide my tracking URL from the status bar, but I cant get the getElementById to trace the id "hide-link", because( I Think ? ) I'm placing the id inside a previous alert function. The Function (1) is keyboard shortcut function which activates the alert box, k2p is the modified alert box css. The last code snippet is my current function, that I'm trying to implement the hide-link into, Function hide()

    My ad plays an audio.mp3 file, listeners hear a message to press a keyboard key, the alert function opens center screen with a URL to click, that URL contains the tracking as well, which is what I'm trying to hide from the status bar.



    HTML Code
    
    <a href="http://www.google.com/" id="hide-link">Go To Google</a>
    
    Code (markup):
    Javascript Code
    <script type="text/javascript">
    function hide()  {
    	document.getElementById("hide-link").onclick=function(e) {
    		jx("counter.php?url="+escape("http://www.google.com/"));
    	}
    }
    window.onload=hide;
    </script>
    Code (markup):
    Current function unedited.
    shortcut.add('<?=$_GET['key2page']?>',function(){k2p.alert('<img src="http://aav.audio-adverts.com/ads/img/visit.png" border="0"> <a href="http://www.audio-adverts.com"><b>NetAudioAds</b></a><br><small><em>Key2page Audio Advertisement.</em></small><br><br><b><a id="hidelink" target="_blank" href="<?=$_GET['clickurl']?>">Click here to visit Advertiser</a></b>');});
    
    Code (markup):
     
    Marty_, Oct 8, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    right. thats ok. if you run hide() onload then it will evaluate DOM via document.getElementById("hide-link") as it is. The problem you have is, "hide-link" is not a part of the dom until later when you add it via your shortcut.add method. You cannot pre-define events like click based on ids that do not exist.

    instead of window.onload=hide, add to shortcut.add at end:

    shortcut.add('<?=$_GET['key2page']?>', function() {
        k2p.alert('<img src="http://aav.audio-adverts.com/ads/img/visit.png" border="0"> <a href="http://www.audio-adverts.com"><b>NetAudioAds</b></a><br><small><em>Key2page Audio Advertisement.</em></small><br><br><b><a id="hidelink" target="_blank" href="<?=$_GET['clickurl']?>">Click here to visit Advertiser</a></b>'); // end alert.
        // hide link event can now take place
        hide();
    });
    PHP:
    also you can do be safe ...
    if (!document.getElementById("hide-link")) return false;
     
    dimitar christoff, Oct 8, 2008 IP