473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem detecting URL in the clicked links

manuelgk
I have another problem. I want to catch the click event, I put certain code and it works well with my code but with a little problem, I already catch the click event and the links too, but when I try to know or shows the URL of the clicked element appear all the URLs of the other links if we have more than one in the html document. This is my code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title></title>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5.  
  6.     <style type="text/css">
  7.       .red_text {
  8.         color:#600;
  9.       }
  10.  
  11.      .green_text {
  12.         color:#060;
  13.      }
  14.  
  15.      .blue_text {
  16.        color:#006;
  17.      }
  18.     </style>
  19.  
  20.     <script type="text/javascript">
  21.       window.onload=function() {
  22.         els=document.body.getElementsByTagName('*');
  23.           for(c=0;c<els.length;c++){
  24.             els[c].onclick=function(){
  25.               getOnclicks(this.tagName,this.id,this.className);
  26.             }
  27.           }
  28.       }
  29.  
  30.       function getOnclicks(el,id,cl) {
  31.           var links = document.links;
  32.           if(id==''){
  33.               id='it has no id set';
  34.           }
  35.           if(cl=''){
  36.               cl='it has no class set';
  37.           }
  38.           if(el='href'){
  39.          for(var i=0;i<links.length;i++)
  40.                alert(links[2].getAttribute('href'));
  41.           }
  42.       }
  43.      </script>
  44.   </head>
  45.  
  46.   <body>
  47.     <h1 id="header" class="red_text">header</h1>
  48.     <div id="foo" class="green_text">some text</div>
  49.     <div class="blue_text">some more text</div>
  50.     <div id="image_container"><img id="my_image" src="my_image.gif" alt="my_image"/></div>
  51.     <a href="http://www.uv.mx" id="hyperlink">UV</a>
  52.     <a href="http://www.google.com" id="hyperlink">Google</a>
  53.     <a href="http://www.yahoo.com.mx" id="hyperlink">Yahoo Mexico</a>
  54.     <a href="http://www.itsx.edu.mx" id="hyperlink">Tec Xalapa</a>
  55.   </body>
  56. </html> 

Thanks everybody
Oct 1 '07 #1
7 1467
acoder
16,027 Expert Mod 8TB
On lines 35 and 38, you're setting rather than testing/comparing. Use double equals.
Oct 1 '07 #2
On lines 35 and 38, you're setting rather than testing/comparing. Use double equals.
Thank you, but when I put a doble equals in line 38 (el=='href') the alert doesn't appear :(, but if I put "el='href' ", or just el, the alert message appear, so what I want to do is show the URL in the alert but just the URL where I clicked and not all the hypertext URL.
Oct 1 '07 #3
acoder
16,027 Expert Mod 8TB
Thank you, but when I put a doble equals in line 38 (el=='href') the alert doesn't appear :(, but if I put "el='href' ", or just el, the alert message appear, so what I want to do is show the URL in the alert but just the URL where I clicked and not all the hypertext URL.
el='href' will always return true. In any case, the tag name is 'A', not 'href'.

One other thing which won't solve your problem, but I'll mention anyway: ids should be unique. You're using the same id for multiple link elements.
Oct 1 '07 #4
Ok, I'm sorry but I forgot to mention that I don't use class and ids anymore, so my code is like this:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title>Prueba</title>
  4.  
  5.     <script type="text/javascript">
  6.       window.onload=function() {
  7.         els=document.body.getElementsByTagName('*');
  8.           for(c=0;c<els.length;c++){
  9.             els[c].onclick=function(){
  10.               getOnclicks(this.tagName);
  11.             }
  12.           }
  13.       }
  14.  
  15.       function getOnclicks(el) {
  16.           var links = document.links;
  17.           if(el='href'){
  18.          for(var i=0;i<links.length;i++)
  19.                alert(links[i].getAttribute('href'));
  20.           }
  21.       }
  22.      </script>
  23.   </head>
  24.  
  25.   <body>
  26.     <h1>header</h1>
  27.     <a href="http://www.uv.mx">UV</a>
  28.     <a href="http://www.google.com">Google</a>
  29.     <a href="http://www.yahoo.com.mx">Yahoo Mexico</a>
  30.     <a href="http://www.itsx.edu.mx">Tec Xalapa</a>
  31.   </body>
  32. </html> 

As you see I take it from an example of a book and internet, so if the thing that I want to do is impossible to do in JS, please let me know it.
Kind regards
Oct 1 '07 #5
acoder
16,027 Expert Mod 8TB
Change your code to this:
Expand|Select|Wrap|Line Numbers
  1. window.onload=function() {
  2.         els=document.body.getElementsByTagName('*');
  3.           for(c=0;c<els.length;c++){
  4.             els[c].onclick=function(){
  5.               getOnclicks(this);
  6.             }
  7.           }
  8.       }
  9.  
  10.       function getOnclicks(el) {
  11.                if (el.tagName=="A") alert(el.getAttribute('href'));
  12.       }
  13.  
Notice the changes I have made. Instead of passing the tag name, just pass the element itself.
Oct 1 '07 #6
Thanks I appreciate your help again it was really usefull, I have a lot to learn about JS!!!. Sorry if I bug you a lot XD.
Kind regards
Oct 2 '07 #7
acoder
16,027 Expert Mod 8TB
That's no problem. Post again if you have any more questions.
Oct 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike | last post by:
In my previous post, I wrote: > ... > GOAL: (very simple) Provide a hyperlink which, when clicked, > calls a javascript function which opens a new URL. > ... > PROBLEM: The following code...
20
by: Mandy Memphis | last post by:
If I perform a mousedown within a document, move the mouse outside the browser window, and then release the mouse button, the document.onmouseup event does not fire. Is there any way to detect a...
3
by: Randy | last post by:
Hello, How would one go about detecting which row/column was clicked in a datagrid. I'm sure it can be done but I can't figure it out. Thanks
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
1
by: Roz Lee | last post by:
I have a page with two textboxes and two buttons which post back to the server. I want to detect which button has been clicked for testing purposes preferably clientside, but otherwise serverside....
3
by: whapts3 | last post by:
I'm putting together a rather dynamic page (AJAX/all-that-mess). So... to go between different sorts of content on the page, I have it set up something like this. <a href="#10"...
5
by: Magician | last post by:
Hello. I am trying to set the onclick event for images through a function, but the event is triggered as soon the page loads, then will not work when the image is clicked. Can anyone suggest what...
8
by: DJA | last post by:
Hello, I site I designed has links that don't seem to work in mac IE 5.2. In my site I'm using CSS to imitate image buttons by using the background image property along with a little extra...
3
by: Prince of Code | last post by:
Hey all, I am trying to track the links that are clicked on a particular web page. Like a typical senario will be: Track (Count) how many times the main links Home,Help,About,Login etc are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.