cancel browser widget navigation - possible?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 1:57 am

Is anyone aware of a way to cancel navigation in the browser widget? I want to detect attempts to navigate away from my app in the browser widget and handle these differently (for example, opening them in another browser instance). In my app these navigations normally target a new tab/window, but in browser widget they navigate away from my single page app.

I'm looking for Livecode (rather than Javascript) solutions to this issue.

I found a discussion on same topic (to find it, search google: browser widget,"cancel navigation",livecode ) I can't post direct URL here as forum rules prohibit posting URLs to the forum (why? very annoying...)

Seems like not being able to detect/cancel/manage navigation in the browser widget is an issue that should be addressed....

~ Rodney

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: cancel browser widget navigation - possible?

Post by capellan » Tue Oct 23, 2018 3:47 am

Hi Rodney,

Are you writing about this link?
http://runtime-revolution.278305.n4.nab ... 21826.html

Al

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 8:04 am

Yes that's the previous discussion on this topic I found - and the proposed solutions are pretty hacky and impractical IMO.

(Re adding links to posts, I think until I have posted enough times to this forum it is suppressing my ability to add hyperlinks to posts...)

~ R

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 10:55 am

One thing I have discovered I can do is monitor the popstate event by adding a listener thus:

put "window.onpopstate = function(event) {" & return &\
"liveCode.jsCallback('popstate',document.location);" & return&\
"}" into tScript
... then do tScript in widget.


Once called as user navigates around content within my SPA, I will get events returned to my jsCallback handler as an array. This interested me as I thought I would just get the location, instead it seems to return the event array (haven't figured out why yet).

on jsCallback pMessage,pValue
...
switch pMessage
case "popstate"
#pValue is returned object
set the label of stack "ob3-study" to "OB3 - "& pValue["hash"]

Unfortunately this won't detect navigation AWAY from my app. But it's still a useful step forwards...

~ Rodney

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 11:33 am

Executing this in Chrome console within a website will prompt you if you attempt to navigate away:

window.addEventListener('beforeunload', function (e) {
var confirmationMessage = '\o/';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});

Has anyone been able to get the beforeunload event to work with the browser widget?

~ Rodney

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: cancel browser widget navigation - possible?

Post by bogs » Tue Oct 23, 2018 2:11 pm

rodneyt wrote:
Tue Oct 23, 2018 1:57 am
I found a discussion on same topic (to find it, search google: browser widget,"cancel navigation",livecode ) I can't post direct URL here as forum rules prohibit posting URLs to the forum (why? very annoying...)
I believe that rule is a way to help deal with bots and spam, since I see you now have 11 posts, that restriction should be lifted on your account, I think.
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: cancel browser widget navigation - possible?

Post by [-hh] » Tue Oct 23, 2018 8:42 pm

Simply target your links to a new window (target="_blank").
Then the widget doesn't follow the link.
But if you run the same htmltext in an ordinary browser the link navigates away.
shiftLock happens

AndyP
Posts: 614
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: cancel browser widget navigation - possible?

Post by AndyP » Tue Oct 23, 2018 8:54 pm

This works for me. Replace the site URL with yours.

In the Browser widget;

on browserNavigateBegin pUrl
if "2108.co.uk" is not among the words of pUrl then
exit to top
end if
end browserNavigateBegin
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: cancel browser widget navigation - possible?

Post by [-hh] » Tue Oct 23, 2018 9:10 pm

rodneyt wrote:Seems like not being able to detect/cancel/manage navigation in the browser widget is an issue that should be addressed ....
Why?
The widget is a "core" web engine. What you want is done by different browser flavours, that is in your app you have also to handle this by yourself. If LiveCode doesn't support this by widget messages you can still do that in JavaScript.
TMHO, adding all such navigation features (that require short javascript callback cycles) to the widget would make the widget slower.
shiftLock happens

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 9:25 pm

Hi Andy,

This detects navigation away but exiting to top doesn't stop the navigation (at least not for me)

e.g.

on browserNavigateBegin pUrl
if not (pUrl contains "DOMAIN") then
answer "Navigating away to:" && pURL
exit to top
end if

... for me this shows answer dialog once pURL is already displayed.
AndyP wrote:
Tue Oct 23, 2018 8:54 pm


on browserNavigateBegin pUrl
if "2108.co.uk" is not among the words of pUrl then
exit to top
end if
end browserNavigateBegin

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 9:31 pm

Thanks for the suggestion. That is the default behaviour in my web app (I am already targeting _blank on my URLs):
Image

This does not stop widget from following links (tested on OSX) If this is working for you can you provide a sample stack that illustrates?
[-hh] wrote:
Tue Oct 23, 2018 8:42 pm
Simply target your links to a new window (target="_blank").
Then the widget doesn't follow the link.
But if you run the same htmltext in an ordinary browser the link navigates away.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: cancel browser widget navigation - possible?

Post by [-hh] » Tue Oct 23, 2018 10:05 pm

Make a new stack with a browser widget and a button.

This is the effect here:
The shiftKey-down-htmltext does nothing when the link is clicked
the shiftKey-up-htmltext navigates to the linked URL.

This works also in the sample stack "TextEditFull" I made using a browser widget.
There an inserted link is not allowed to navigate away from the editor.

Code: Select all

on mouseUp
  if the shiftkey is down then
    set htmltext of widget "browser" to \
          "<html><body><a href='http://hyperhh.de' target='_blank'>Hi</a></body</html>"
  else
    set htmltext of widget "browser" to \
          "<html><body><a href='http://hyperhh.de'>Hi</a></body</html>"
  end if
end mouseUp
shiftLock happens

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Tue Oct 23, 2018 10:12 pm

I suggest reading over thread http://runtime-revolution.278305.n4.nab ... 21826.html which sets out the issues quite well. I'm just asking for a way to be able to effectively cancel navigation away. For example, if "exit to top" within browserNavigateBegin actually worked to cancel navigation, that would be fine.
[-hh] wrote:
Tue Oct 23, 2018 9:10 pm
rodneyt wrote:Seems like not being able to detect/cancel/manage navigation in the browser widget is an issue that should be addressed ....
Why?
The widget is a "core" web engine. What you want is done by different browser flavours, that is in your app you have also to handle this by yourself. If LiveCode doesn't support this by widget messages you can still do that in JavaScript.
...

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: cancel browser widget navigation - possible?

Post by [-hh] » Tue Oct 23, 2018 10:40 pm

This will certainly work.

Code: Select all

on browserNavigateBegin pURL
   -- you can't catch the htmltext here, this is already the new one
   -- if pURL is not empty then set the URL of me to empty --> CANCELS (optionally)
   set htmltext of me to myHTML --> whatever is YOUR current (saved) htmltext
end browserNavigateBegin
shiftLock happens

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: cancel browser widget navigation - possible?

Post by rodneyt » Wed Oct 24, 2018 12:08 am

... but is not a viable option for me as I am working with a SPA (single page app) which dynamically changes content (eg as synchronous changes happen or as user scrolls window).
[-hh] wrote:
Tue Oct 23, 2018 10:40 pm
This will certainly work.

Code: Select all

on browserNavigateBegin pURL
   -- you can't catch the htmltext here, this is already the new one
   -- if pURL is not empty then set the URL of me to empty --> CANCELS (optionally)
   set htmltext of me to myHTML --> whatever is YOUR current (saved) htmltext
end browserNavigateBegin

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”