Results 1 to 8 of 8

Thread: Javascript code that reads if a link has been clicked

  1. #1
    Join Date
    Jun 2010
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    HTML 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. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    2 things;

    what needs to be read when what is clicked? (and what do you mean exactly when you say "read by javascript"?)

    Also, how can you add javascript if you cant edit the CMS header?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Thinking on, do you mean an onclick event in javascript, like this?: http://www.w3schools.com/jsref/tryit...yjsref_onclick

    If so, what exactly do you want to achieve "onclick"?

    The previous question still stands too though - how can you add javascript if you cant edit the CMS? Or do you mean something else?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  4. #4
    Join Date
    Jun 2010
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Beverleyh View Post
    Thinking on, do you mean an onclick event in javascript, like this?: http://www.w3schools.com/jsref/tryit...yjsref_onclick

    If so, what exactly do you want to achieve "onclick"?

    The previous question still stands too though - how can you add javascript if you cant edit the CMS? Or do you mean something else?
    Hello

    The code you see in this topic, is a plugin in the CMS i work on, I cannot edit this code. But i do have access to the header and i need the JS to read the clicks on the link or div id.

    Here is some JS i've been working on:
    <script type="text/javascript">
    document.getElementById("Button").addEventListener("click", callback, true);

    function callback() {
    alert("clicked");

    return false;
    }
    </script>

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Is this resolved then? was it just the javascript onclick event that you needed help identifying?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. #6
    Join Date
    Jun 2010
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Beverleyh View Post
    Is this resolved then? was it just the javascript onclick event that you needed help identifying?
    Hello, no not yet

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That won't work in the head because the button doesn't exist yet. If the page already uses jQuery, this should work:

    Code:
    <script type="text/javascript">
    jQuery(function($){
    	$('#Button').click(function(e){
    		alert('clicked');
    		return false;
    	});
    });
    </script>
    I say should, because if the CMS has already assigned it an on click function, that may still fire and may fire first. And that may vary by browser.

    A rough equivalent if jQuery isn't on the page would be:

    Code:
    <script type="text/javascript">
    if (window.addEventListener){
    	window.addEventListener('load', function(){
    		document.getElementById('Button').addEventListener('click', function(e){alert('clicked');e.preventDefault();}, true);
    	}, false);
    }
    else if (window.attachEvent){
    	window.attachEvent('onload', function(){
    		document.getElementById('Button').attachEvent('onclick', function(){alert('clicked');event.returnValue = false;});
    	});
    }
    </script>
    But again, if the CMS has already assigned an on click event to it, that may take precedence. And that may vary by browser.

    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2010
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    That won't work in the head because the button doesn't exist yet. If the page already uses jQuery, this should work:

    Code:
    <script type="text/javascript">
    jQuery(function($){
    	$('#Button').click(function(e){
    		alert('clicked');
    		return false;
    	});
    });
    </script>
    I say should, because if the CMS has already assigned it an on click function, that may still fire and may fire first. And that may vary by browser.

    A rough equivalent if jQuery isn't on the page would be:

    Code:
    <script type="text/javascript">
    if (window.addEventListener){
    	window.addEventListener('load', function(){
    		document.getElementById('Button').addEventListener('click', function(e){alert('clicked');e.preventDefault();}, true);
    	}, false);
    }
    else if (window.attachEvent){
    	window.attachEvent('onload', function(){
    		document.getElementById('Button').attachEvent('onclick', function(){alert('clicked');event.returnValue = false;});
    	});
    }
    </script>
    But again, if the CMS has already assigned an on click event to it, that may take precedence. And that may vary by browser.

    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

    Thanks a lot for this John I will give it a go and feed back.

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 06:07 AM
  2. what link was clicked a div.
    By riptide in forum JavaScript
    Replies: 16
    Last Post: 07-23-2008, 08:17 PM
  3. Animation when a link is clicked
    By Wimpie in forum JavaScript
    Replies: 0
    Last Post: 10-10-2007, 07:17 AM
  4. anchor link in javascript code
    By jianxin9 in forum JavaScript
    Replies: 4
    Last Post: 07-12-2007, 01:35 PM
  5. How to scroll-up page when link in a tab clicked
    By viens in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 03-02-2007, 07:33 PM

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
  •