[Greasemonkey] halting navigation via Javascript

chris feldmann cfeldmann at gmail.com
Wed Oct 5 12:23:02 EDT 2005


On 10/5/05, Matthew Weymar <matthew.weymar at gmail.com> wrote:
> On 10/5/05, chris feldmann <cfeldmann at gmail.com> wrote:
> >
> > On 10/5/05, Matthew Weymar <matthew.weymar at gmail.com> wrote:
> > > Hi,
> > >
> > > I'm told, but just wanted to confirm that there's no way to use
> > Javascript
> > > to stop a browser from navigating away from a given page.
> > >
> > > I know that it's possible to intervene between the request to navigate
> > away
> > > and the actual move to another page, but there's really no way to stop
> > this
> > > move in its tracks?...
> > >
> > > Tks,
> > > Matthew
> >
> > The question ispeople will ask is, "why would you want to?" Unless
> > you're doing it to yourself...
>
>
> Precisely. More on this below.
>
> Maybe something like
> > var ref = document.location.href;
> > window.onunload = function(){
> > document.location.href = ref;
> > };
>
>
> What you're getting at would reload the current page?... That wouldn't do it
> for me. (More below.)
>
> I need to - or at least *think* I need to - stop the unloading-and-loading
> process.
>
> But this REALLY violates users expectations to say the least.
>
>
> The immediate goal is to idiot-proof TiddlyWiki so that if I, for example,
> inadvertently request a search using the search bar while editing my TW
> file, I don't lose content in "tiddlers" I've not yet closed. (TW does save
> in this situation, but only content in tiddlers that have been closed.
> Reloading the page would sacrifice all changes.)
>
> This is a more general problem, of course. There are other applications in
> which one might stand to lose content by inadvertently navigating away from
> a page prior to saving. I can imagine a script that would check, at least
> certain 'sites, for open forms before allowing a new page to load. If open
> forms were found, the user could be prompted, "Are you sure you want to
> abandon changes to this form?..."
>
> If worse comes to worse, all open forms could be handled appropriately, on a
> 'site by 'site basis - i.e., closing tiddlers, saving Gmail drafts, etc. -
> before loading the new page, if this load were inevitable.
>
> Tks for your reply,
> Matthew


tiddles? This esoteric jargon is beyond me! BUT, you could iterate
through the page with an xpath query and add an event listener to all
the anchor elements. Then preventDefault() and have your way with
them. Maybe add a confirm()?







> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list