We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

huangs3
huangs3 asked
on
Medium Priority
773 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

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

Author

Commented:
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.

Author

Commented:
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.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
James MurrellQA Product Specialist
CERTIFIED EXPERT

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.