sbos
sbos

Reputation: 307

Handle url change by javascript

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?

Upvotes: 0

Views: 968

Answers (3)

Felipe
Felipe

Reputation: 1300

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

Upvotes: 0

Nabab
Nabab

Reputation: 2644

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.

Upvotes: 1

David Mårtensson
David Mårtensson

Reputation: 7600

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.

Upvotes: 0

Related Questions