• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 732
  • Last Modified:

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
0
huangs3
Asked:
huangs3
  • 3
  • 2
1 Solution
 
DesignbyonyxCommented:
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

0
 
huangs3Author 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.
0
 
DesignbyonyxCommented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
huangs3Author 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.
0
 
DesignbyonyxCommented:
hmm, the only thing I can suggest is to leverage the URL hash to achieve your goal.  Do you have any control over the "external program"?  The program would simply use url's beginning with the hash sign:

#/page1.html
#/page2.html

So that website A would have a "history" like this:

http://www.website-a.com/  (first load)
http://www.website-a.com/#/page1.html  (url changed by "external program")
http://www.website-a.com/#/page2.html  (url changed by "external program")

Then with javascript, you have to set an interval which periodically checks window.location.href to see if it has changed.  If so, grab the hash and take action.  Hope that helps.

If you are using jQuery, there is a hash change plugin, which I would highly recommend:

http://benalman.com/projects/jquery-hashchange-plugin/
0
 
James MurrellProduct SpecialistCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now