0

Is it possible to handle URL change when some link on web page is being clicked to get the new URL and somehow process it?

Also can I do the same thing but on some iframe inside my page? I know that I can't execute any javascript on iframe if it's url is not on my domain, but maybe it's possible to just get the new pending url of iframe.

If the second problem requries some server-side solutions then what is the most accurate and right one?

sbos
  • 209
  • 4
  • 11

3 Answers3

1

You better use a JS library if you want all your links to be handled by JavaScript.

E.g. with jQuery:

$("a").click(function(){
  var link = $(this).attr("href");
  /* Here your function
  In this example I'm just following the link. */
  document.location=link; 
  return false;
});

I don't think you can capture events from an iframe having its source on another domain.

Nabab
  • 2,418
  • 16
  • 29
  • A library certainly isn't required to handle all link click events. Those events propagate through the ancestors all the way up the document tree and can be handled at any stage. Search "event bubbling" for more information. – Andy E Feb 10 '11 at 12:16
  • Not required sure, just easier. – Nabab Feb 10 '11 at 13:32
0

You cannot access the IFRAME in any way unless its in the same domain except to send it to a new page, but no you cannot get the current URL from the iframe AFAIK.

Regarding URL change in your page, you could either add an event to all links to call a function before switching or use onbeforeclose event to do something.

David Mårtensson
  • 7,235
  • 4
  • 28
  • 47
0

You can capture url fragment changes on iframe. If you have access to the code of iframe, you can make it work. Example here: http://www.tagneto.org/blogcode/xframe/ui.html

Felipe
  • 1,280
  • 10
  • 11