Link to home
Start Free TrialLog in
Avatar of huangs3
huangs3Flag for Canada

asked on

javascript: how to trap the URL change before it actually unload the current webpage?

Hi Experts:

    I want to make a webpage named "MainWebPage" such that when I open it from javascript of some external webpage and change its URL, the "MainWebPage" can detect what is going on and stop unloading the current page, instead it process the new URL and do something in javascript correspondingly.

    I haven't find out how to do that yet.
    If I use the window.onbeforeunload event of the "MainWebPage" window, I will at least need to figure out two things: 1) how to decide whether user is closing the window or changing the URL? 2) if the URL is changing, how to stop the unload of the current page in the "MainWebPage" window?

    Please let me know if you have any idea.
    Thank you.


My development environment:
-- .NET 4
-- IE 7
-- Silverlight 4
-- Windows XP SP3
-- Visual Studio 2010
Avatar of Designbyonyx
Designbyonyx
Flag of United States of America image

A fortunate feature of the DOM event model is that most events bubble up the DOM tree.  What you can do is attach an "onclick" event to the root document object.  Then you can check the event target to see if it's an "A" tag (anchor link).  If it is a link, you can interrupt it's normal behavior and inject your own behavior.

document.onclick=function(e){
  var evt = window.event || e;

  if (!evt.target) {
    evt.target = evt.srcElement;
  }

  if(evt.target.nodeName === 'A') {
    // Put your custom code here

    // Prevent defualt behavior of link
    if (evt && evt.preventDefault) {
      evt.preventDefault();
    } else {
       return false;
    }
  }
}

Open in new window

Avatar of huangs3

ASKER

Hi Designbyonyx:

    Unfortunately trapping the click-event does not fit my situation, because the URL change in my "MainWebPage" is not done by user clicking links on it...

    The way that "MainWebPage" get changed may be done by another IE window running javascript like:
=================================================
win = window.open("", "MainWebPage");
win.location = "www.xyz.com";
=================================================

    Is it possible?
    Thank you.
So I guess I am a little unclear as to what your are wanting.  Can you please describe what you want a little clearer... something like:

I have a master "parent" page with two iframes.  Iframe A will cause Iframe B to load a new page.  I want the "parent" page to intercept this page load and perform some action...

Thanks.
Avatar of huangs3

ASKER

Hi Designbyonyx:

    I will phase my question this way:
    "I have a webpage A. Some external program will get its handler and change its URL. I want this webpage A to intercept its own URL change and perform some action."
    Is that doable?
    Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Designbyonyx
Designbyonyx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Murrell
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.