Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript code that reads if a link has been clicked

    I have a simple bit of code (contained in a CMS and cant be edited) that and needs to be read by Javascript in the header if it has been clicked.

    Here is the code:

    Code:
    <div id="example" class="page-element-outer" style="text-align: left"><div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div></div>
    Can you assist?

    C

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Quote Originally Posted by jelly46 View Post
    I have a simple bit of code (contained in a CMS and cant be edited) that and needs to be read by Javascript in the header if it has been clicked.

    Here is the code:

    Code:
    <div id="example" class="page-element-outer" style="text-align: left"><div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div></div>
    Can you assist?

    C
    Is that piece of code the part that cannot be edited?

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello

    Yes it is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Is this what you mean?

    Code:
    <div id="example" class="page-element-outer" style="text-align: left">
    <div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div>
    </div>
    
    <script type = "text/javascript">
    
    var mydiv = document.getElementById('Button');
    if (mydiv.addEventListener) {  // all browsers except IE before version 9
    mydiv.addEventListener("click", listener, false);
    }
    
    else {
    if (mydiv.attachEvent) {   // IE before version 9
    mydiv.attachEvent ("onclick", listener);
    }
    }
    
    function listener() {
    alert (document.getElementById("Button").src)
    }
    
    
    </script>
    Quizmaster: Which European capital is divided into 20 arrondissements?
    Contestant: Sweden.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think so, thanks Phillip

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    How is that going to work if it must be in the <head>
    and you can not change anything in the <body>?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Just have to do it at window.onload time.

    Presumably, you don't want to interfere with any other events that are triggered by window.onload, so you have to do it in steps.

    Maybe like this?
    Code:
    <head>
    <script type="text/javascript">
    function addEvent( addTo, eventName, listener )
    {
        if ( addTo.addEventListener != null )
        {
            addTo.addEventListener( eventName, listener, false ); 
        } else {
            addTo.attachEvent ("on" + eventName, listener);
        }
    }
    
    function myButtonClickHandler( )
    {
        alert( this.id + " was clicked" ); // or whatever real code you want to use
    }
    
    function myOnLoadHandler( )
    {
        var mydiv = document.getElementById('Button');
        addEventr( myDiv, "click", myButtonClickHandler );
    }
    
    addEvent( window, "load", myOnLoadHandler );
    </script>
    No?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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