Solved

Detecting the link click in an iframe

Posted on 2004-09-17
9
442 Views
Last Modified: 2008-02-01
Hi All,

I can detect the link clicks with "document.onclick=function()" function but there is an iframe in the document and i want to detect the link clicks in that iframe also. I tried "document.getElementById("innerframeID").onclick=function()" but it didn't work.

How can i perform this?

Thanks!
0
Comment
Question by:blacklord
  • 4
  • 2
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
This isn't possible as far as I know.

The iframe is a whole separate document.

window.onclick functions the same as document.onclick and doesn't do anything.
Putting an onclick handler inside the iframe tag is fruitless because the iframe is considered a separate document.
Notice how if you have an onclick inside the iframe, and you click the border of the iframe, the action executes? That's what handlers in the iframe do.

Because the iframe is a different document, there's nothing you can do.

You can use a server-side language like PHP or ASP to include the file into your page. It won't be an iframe then and will be part of the document. Or you can use XMLHTTP client side, which although works, is only supported by recent browsers, for example:

<script language="javascript" type="text/javascript">
<!--

function getDoc(path) {
   if (document.all) {
      var xObj=new ActiveXObject("Microsoft.XMLHTTP");
      xObj.open("GET",path,false);
      xObj.send();
      return(xObj.responseText);
   } else {
      var xObj=new XMLHTTPRequest();
      xObj.open("GET",path,false);
      xObj.send(null);
      return(xObj.responseText);
   }
}

//-->
</script>

which will return the contents of the page specified in path.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
I can show hot that event capture works, but I need more details.
First question: why does not the page in the iframe react to the clicks itself?
Second question: where is the function which has to be called in onClick handler? Is it the main parent document?
And last but most important: do you want to handle the click when anywere on the iframe is clicked or only when some links or other elements are clciked?

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Hello Zyloch. Do you have again sleeping problems? Now you are in my time zone :-)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
Comment Utility
And this is for Zyloch only:

<html>
<head>
<script>
function myFunction(){
  e=frames.innerframeID.event;
  window.status=e.clientX+":"+e.clientY;
}
</script>
</head>
<body>
<h1>Look into status bar</h1>
<iframe name="innerframeID" onLoad="frames.innerframeID.document.onmousemove=myFunction" height="100%" width="100%">
</iframe>
</body>
</html>

0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Wow, I never knew that. Shows how much I know, eh? lol

Thanks Zvonko, once again, for showing me the light. And now, I believe I can fall asleep peacefully :-) (no, this is not sarcasm, I'm just tired)

O.O -> o.o -> -_- -> x_x

Till later,
Zyloch
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Good Nigth :-)
0
 
LVL 9

Expert Comment

by:cwolves
Comment Utility
<iframe id="ifr" src="page.html"></iframe>
<script language="javascript">
frames.ifr.document.onclick=function(){alert('test');}
</script>
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now