Using the HTML5 window.history API, I can control the navigation pretty well on my web app.
The app currently has two states: selectDate (1) and enterDetails (2).
When the app loads, I replaceState and set a popState listener:
history.replaceState({stage:"selectDate",...},...);
window.onpopstate = function(event) {
that.toStage(event.state.stage);
};
When a date is selected and the app moves to stage 2 I push state 2 onto the stack:
history.pushState({stage:"enterDetails",...},...);
This state is replaced anytime details change so they are saved in the history.
There are three ways to leave stage 2:
save (AJAX submit)
cancel
back button
The back button is handled by the popstate listener. The cancel button pushes stage 1 so that the user can go back to the details they were entering the back button. These both work well.
The save button should revert back to stage 1 and not allow the user to navigate back to the details page (since they already submitted). Basical, y it should make the history stack be length = 1.
But there doesn't seem to be a history.delete(), or history.merge(). The best I can do is a history.replaceState(stage1) which leaves the history stack as: ["selectDate","selectDate"].
How do I get rid of one layer?
Edit:
Thought of something else, but it doesn't work either.
history.back(); //moves history to the correct position
location.href = "#foo"; // successfully removes ability to go 'forward',
// but also adds another layer to the history stack
This leaves the history stack as ["selectDate","selectDate#foo"].
So, as an alternative, is there a way to remove the 'forward' history without pushing a new state?
You may have moved on by now, but... as far as I know there's no way to delete a history entry (or state).
One option I've been looking into is to handle the history yourself in JavaScript and use the window.history object as a carrier of sorts.
Basically, when the page first loads you create your custom history object (we'll go with an array here, but use whatever makes sense for your situation), then do your initial pushState. I would pass your custom history object as the state object, as it may come in handy if you also need to handle users navigating away from your app and coming back later.
var myHistory = [];
function pageLoad() {
window.history.pushState(myHistory, "<name>", "<url>");
//Load page data.
}
Now when you navigate, you add to your own history object (or don't - the history is now in your hands!) and use replaceState to keep the browser out of the loop.
function nav_to_details() {
myHistory.push("page_im_on_now");
window.history.replaceState(myHistory, "<name>", "<url>");
//Load page data.
}
When the user navigates backwards, they'll be hitting your "base" state (your state object will be null) and you can handle the navigation according to your custom history object. Afterward, you do another pushState.
function on_popState() {
// Note that some browsers fire popState on initial load,
// so you should check your state object and handle things accordingly.
// (I did not do that in these examples!)
if (myHistory.length > 0) {
var pg = myHistory.pop();
window.history.pushState(myHistory, "<name>", "<url>");
//Load page data for "pg".
} else {
//No "history" - let them exit or keep them in the app.
}
}
The user will never be able to navigate forward using their browser buttons because they are always on the newest page.
From the browser's perspective, every time they go "back", they've immediately pushed forward again.
From the user's perspective, they're able to navigate backwards through the pages but not forward (basically simulating the smartphone "page stack" model).
From the developer's perspective, you now have a high level of control over how the user navigates through your application, while still allowing them to use the familiar navigation buttons on their browser. You can add/remove items from anywhere in the history chain as you please. If you use objects in your history array, you can track extra information about the pages as well (like field contents and whatnot).
If you need to handle user-initiated navigation (like the user changing the URL in a hash-based navigation scheme), then you might use a slightly different approach like...
var myHistory = [];
function pageLoad() {
// When the user first hits your page...
// Check the state to see what's going on.
if (window.history.state === null) {
// If the state is null, this is a NEW navigation,
// the user has navigated to your page directly (not using back/forward).
// First we establish a "back" page to catch backward navigation.
window.history.replaceState(
{ isBackPage: true },
"<back>",
"<back>"
);
// Then push an "app" page on top of that - this is where the user will sit.
// (As browsers vary, it might be safer to put this in a short setTimeout).
window.history.pushState(
{ isBackPage: false },
"<name>",
"<url>"
);
// We also need to start our history tracking.
myHistory.push("<whatever>");
return;
}
// If the state is NOT null, then the user is returning to our app via history navigation.
// (Load up the page based on the last entry of myHistory here)
if (window.history.state.isBackPage) {
// If the user came into our app via the back page,
// you can either push them forward one more step or just use pushState as above.
window.history.go(1);
// or window.history.pushState({ isBackPage: false }, "<name>", "<url>");
}
setTimeout(function() {
// Add our popstate event listener - doing it here should remove
// the issue of dealing with the browser firing it on initial page load.
window.addEventListener("popstate", on_popstate);
}, 100);
}
function on_popstate(e) {
if (e.state === null) {
// If there's no state at all, then the user must have navigated to a new hash.
// <Look at what they've done, maybe by reading the hash from the URL>
// <Change/load the new page and push it onto the myHistory stack>
// <Alternatively, ignore their navigation attempt by NOT loading anything new or adding to myHistory>
// Undo what they've done (as far as navigation) by kicking them backwards to the "app" page
window.history.go(-1);
// Optionally, you can throw another replaceState in here, e.g. if you want to change the visible URL.
// This would also prevent them from using the "forward" button to return to the new hash.
window.history.replaceState(
{ isBackPage: false },
"<new name>",
"<new url>"
);
} else {
if (e.state.isBackPage) {
// If there is state and it's the 'back' page...
if (myHistory.length > 0) {
// Pull/load the page from our custom history...
var pg = myHistory.pop();
// <load/render/whatever>
// And push them to our "app" page again
window.history.pushState(
{ isBackPage: false },
"<name>",
"<url>"
);
} else {
// No more history - let them exit or keep them in the app.
}
}
// Implied 'else' here - if there is state and it's NOT the 'back' page
// then we can ignore it since we're already on the page we want.
// (This is the case when we push the user back with window.history.go(-1) above)
}
}
There is no way to delete or read the past history.
You could try going around it by emulating history in your own memory and calling history.pushState everytime window popstate event is emitted (which is proposed by the currently accepted Mike's answer), but it has a lot of disadvantages that will result in even worse UX than not supporting the browser history at all in your dynamic web app, because:
popstate event can happen when user goes back ~2-3 states to the past
popstate event can happen when user goes forward
So even if you try going around it by building virtual history, it's very likely that it can also lead into a situation where you have blank history states (to which going back/forward does nothing), or where that going back/forward skips some of your history states totally.
A simple solution:
var ismobilescreen = $(window).width() < 480;
var backhistory_pushed = false;
$('.editbtn').click( function()
{
// push to browser history, so back button will close the editor
// and not navigate away from site
if (ismobilescreen && !backhistory_pushed)
{
window.history.pushState('forward', null, window.location);
backhistory_pushed = true;
}
}
Then:
if (window.history && window.history.pushState)
{
$(window).on('popstate', function()
{
if (ismobilescreen && backhistory_pushed && $('.editor').is(':visible'))
{
// hide editor window (we initiate a click on the cancel button)
$('.editor:visible .cancelbtn').click();
backhistory_pushed = false;
}
});
}
Results in:
User opens editor DIV, the history state is saved.
User hits back button, history state is taken into account.
Users stays on page!
Instead of navigating back, the editor DIV is closed.
One issue: If you use a "Cancel" button on your DIV and this hides the editor, then the user has to click the mobile's back button two times to go back to the previous URL.
To solve this problem you can call window.history.back(); to remove the history entry by yourself which actually deletes the state as requested.
For example:
$('.btn-cancel').click( function()
{
if (ismobilescreen && backhistory_pushed)
{
window.history.back();
}
}
Alternatively you could push a URL into the history that holds an anchor, e.g. #editor and then push to history or not if the anchor exists in the recent URL or not.
After I enter "reset", I does not received any message "Welcome Back" and restart the conversation.
bot.dialog('reset', [
function(session) {
session.send("Welcome Back");
session.beginDialog('/');
}]
)
.reloadAction('reset', 'Ok, starting over.', {
matches: /^reset/i,
});
The reloadAction only works for dialogs that have already been added to the stack. In order to use it correctly, the reloadAction needs to be attached to a dialog that has already been called/used previously.
For example, if a user is responding to a series of user profile questions across multiple dialogs (personal info, then address/previous address, followed by education) and the user types 'reset' while in the education dialog, then the reloadAction would trigger having been attached to the first user profile dialog (that collects personal info). Thus, the user would be brought back to the beginning of the user profile dialogs to start over.
If you are intent on calling a new dialog, then you will likely want to use triggerAction. This does clear the dialog stack entirely but also allows you to redirect back to dialog x. In this way, you achieve the same result as in your ask with just a little bit of extra code:
bot.dialog('reset', [
function(session, args, next) {
session.send("Ok, starting over.");
next();
},
function(session) {
session.send("Welcome Back");
session.beginDialog('/');
}]
).triggerAction({
matches: /^reset/i
});
Hope of help!
I am confused with endDialog() and endConversion() when using bot-framework.
What's the difference between them?
If I don't invoke them, just using send()? What constraint will be?
When inside a dialog, you can invoke some other dialog. For e.g.
bot.dialog("/", [
function(session, data, next()){
session.send("Hi");
if(session.message.text === "hello"){
// starts a new dialog
session.beginDialog("helloDialog");
next();
} else {
next();
}
}, function(sesion, data){
session.send("end of root dialog");
}
]);
bot.dialog("helloDialog",[
function(session){
session.send("inside the hello dialog");
session.endDialog(); // explicitly ends the dialog
}
])
When user input is hello, output is
Hi
inside the hello dialog
end of root dialog
When user input is anything else, output is
Hi
end of root dialog
session.endDialog ends the current dialog, and resumes the parent dialog.
session.endConversation ends the conversation itself.
In Technical terms, when a dialog is called, the dialog moves into a stack called dialogStack. When another dialog is called from current dialog, that new dialog is placed at the top of dialogStack. When this new dialog completes its operation, this dialog is popped from the stack, and the last dialog resumes.
When session.endConversation is invoked, the dialog stack is emptied right away (this is a behavior am not fully sure though)
I have a flash movie I am making that has a search box and a search button. The button has this code:
on (release, keyPress "<Enter>") {
searchbox.execute();
/*the function above processes searches*/
}
Clicking on the button works just fine. Pressing Enter doesn't do a bean! Does anyone know why this is, and any ways I can work around it? I'd prefer not to use listeners if I can possibly avoid it at all.
Using on() is a deprecated AS1 practice, so maybe you should just stop using it. Thanks to the onKeyDown event handler of the MovieClip class, it is possible to use proper code to do it without listeners, so you don't have to worry about them. ;)
Anyway, on with the code. Type this into the timeline which contains the button:
//Enable focus for and set focus to your button
searchButton.focusEnabled = true;
Selection.setFocus(searchButton);
//The onRelease handler for the button
searchButton.onRelease = function(){
//You need this._parent this code belongs to the button
this._parent.searchbox.execute();
}
//The onKeyDown handler for the button
searchButton.onKeyDown = function(){
//Key.getCode() returns the key code of the last key press
//Key.ENTER is a constant equal to the key code of the enter key
if(Key.getCode() == Key.ENTER){
this._parent.searchbox.execute();
}
}
I have a modal dialog box presented in Yahoo UI. The user selects a value from dialog "A", and then I want to present another modal dialog box to collect some more data in dialog "B".
I have been using the YAHOO.widget.Dialog successfully. The problem seems to be that you can't initiate dialog window "B" from the handler function of dialog "A". So, how can you programmatically launch a second dialog window after the user hits the "OK" button on the first ?
(I had tried to create an additional Listener for a field that is updated in dialog "A" to trigger dialog "B" but this doesn't work either.)
Thanks..
Check out the documentation: http://developer.yahoo.com/yui/container/dialog/#events. The following code should do the trick:
var firstDialog = new YAHOO.widget.Dialog('firstDialog', { postmethod: "manual" });
firstDialog.manualSubmitEvent.subscribe(function (type, args) {
var nextDialog = new YAHOO.widget.Dialog('nextDialog', { });
/* more configuration stuff... */
nextDialog.render();
nextDialog.show();
});
firstDialog.render();
firstDialog.show();
This handles when the form is to be submitted, which I think what you mean by selects a value, but if not let me know and I can give some help on that situation.