Reset form when navigating within website - javascript

Regarding below:
<html>
<head>
</head>
<body onload="document.naughtyWidgets.reset();">
<form name="naughtyWidgets">
… widgets…
</form>
</body>
</html>
The form resets just fine when the page is refreshed. However, if the form is say under page 'X', and one clicks a couple of radio buttons on the form, navigates to page 'Y', and returns to page 'X', the radio buttons are still clicked. Is there any way to reset these buttons to default when revisiting the form?
Thanks

This typically happens when the user clicks the browser's "back" button. In that case, there really isn't a full page load; it's coming from the cache.
Use the onbeforeunload event to trigger a form reset just before leaving the page.
window.onbeforeunload
From here: Best way to detect when a user leaves a web page?
EDIT:
Quote OP's Comment:
"maybe it helps knowing that the website is made using Wix, and the html part is simply an additional box on a page, in which i can add code."
That's a huge detail you should not have left out of your OP. If you're just sticking content into a content box within a CMS, then this issue is likely impossible to fix without control over the entire page.

Related

display parent back history in iFrame

I want to display a users previously visited page in an iframe on my page. For example: if a user is on cnn.com, and then they type in mysite.com an click on a button on my page, I was the iframe on my page to display cnn.com.
I have read several solutions online but none have fixed my issue. My current implementation is as follows:
index.html:
<body>
<div>
<h1>Welcome to mysite!</h1>
<h3>Click the button to see a site you have previously visited</h3>
<button id="button">magic</button>
</div>
<br>
<p>This is a site you visited previously</p>
<iframe id="myIFrame" height="500px" width="100%" class="netframe" src="example.com"></iframe>
</body>
script.js:
$(document).ready(function(){
$("#button").click(function(e){
document.getElementById("button").style.color = "red";
$("#myIFrame").attr('src', parent.history.back());
});
});
The issue I am having is that when I click the button, the entire page goes back, and it is not shown in the iframe. So in my example when I click the button, my entire page goes from mysite.com to cnn.com. Id like the parent page to remain the same, and the iframe to load cnn.com within it.
I have read up on some of the quirks regarding how history is different depending on how you load up an iframe, but none of that has helped me.
If there a way to do what I want to do?
No, this isn't a thing that you can do. The contents of the user's browser history are generally treated as a secret; you cannot cause the browser to reveal them to you.
In situations where the user navigated to your page by clicking a link on the previous page, you can determine what that page was by checking document.referrer. However, this will not be the case if the user entered your page's URL manually, as you describe in your question.
In any case, methods like history.back() do not return a value; they cause the frame to navigate to the previous page in its history. There is no way to determine what this page is, and doing so will generally cause your script to stop running (as your page is no longer active).

How to hide a transient web page once it's been used?

I have the following situation
Home Page -- User clicks a link, which directs them to a JS-generated page, which may be time intensive
JS-page does some work and may display a "timer" saying work will be done in X seconds, and redirects user to a PDF
PDF page is shown to the user
Problem:
Once the user is done with PDF, they click "Back" button on the browser, hoping to go back to Home Page, but instead they go to JS page, as that's how browsers work -- they step back once. Naturally that displays the timer again and redirects users right back to PDF.
How do I structure my software so that when users are done with PDF, clicking Back (or some other simple way) takes them back to the Home Page?
Simple work-around is for users to click back twice in quick succession, but that's kind of inconvenient - breaks usability of this particular piece for the users and I don't want to do that.
EDIT For Code:
PHP side:
case "export-to-pdf":
$printpdf->generateCanvasInBrowser();
break;
HTML/JS side:
public function generateCanvasInBrowser()
{
//generate form with data
//submit form to PHP script
document.forms['form'].submit();
}
PHP-side:
//Takes and processes POST-ed data from form, and generates PDF
<?
echo printPDF();
EDIT For solution try:
I did a modal dialog with Java Script (https://stackoverflow.com/a/15582060/2883328), and now JS page shows up in an overlay div, but then also does the PDF. It shows up in the dialog box, not in the main browser. It looks fair enough, but still breaks expected functionality for the user as they are used to clicking back from PDF to home page. With this one I'll need to find a way to submit the form AND get out of the modal box.
(moving over from my comment, which seemed to fix the problem mostly)
The best way to fix this is to display the countdown on the home page itself. That would avoid the problem altogether, because then you wouldn't load any page in between. If you use a modal, for example, which contains the js-generated page, then you will need to make sure to submit the form to the top page. You can do that by adding target="_top" to your <form> tag.
I hope this works for you.
Generate the JS Page INSIDE A DIV in the Home page and work widh "visibility: collapse" to show or hide .if you use JQuery use ("#div").hide() and .show()
That should be a lot easier and solve your problem

jQuery - simulate focus on Page

On one of the pages of our website (asp .net web forms), we are only able to tab through the controls of the page 1 time (by pressing tab key) and after that it just cycles tabbing through address bar and page tab. It only happens in IE. We are using this script and that's what's causing it:
<script type='text/javascript' language='JavaScript' src='https://mpsnare.iesnare.com/snare.js'> </script>
When I remove this script, it works correctly and I can tab through the controls of the page as many times as I want. However we need to have this script on this particular page. Now I noticed one thing: once the tabbing stops working (i.e. it is only cycling through address bar and page tab), if I click in an empty area of the page then tabbing starts to work and it then always works permanently. This means, this script is removing the focus from the page. So I think what I want is that, as soon as the above script finishes whatever it is doing, I want to somehow simulate click on the empty area of the page. We are using jQuery as well. Any help will be appreciated, thanks.
Not sure this is your actual solution but you can do this with plain javascript:
document.getElementById("logo").focus()

Hijack Back Button? [duplicate]

I am trying a new functionality for my web site. I want to do simple navigation by hiding/showing <div> elements.
For example, when a user clicks a "details" button on some product, I want to hide the main <div> and show the <div> containing the details for the product.
The problem is that to go back to the previous "page", I have to undo all the display/visibility style changes, which is ok if the user clicks the "close" button in the newly opened <div>. But most users will hit the BACK button.
Is there a way to make the BACK button go back to the previous "state" of the page i.e., undo the visibility/display changes?
Thanks.
Yes. What you're looking for is called AJAX browser history.
There are a few open implementations out there, like RSH as well as plugins/modules for frameworks like jQuery and YUI.
to answer the question of your title (that's what I was looking for)
Using the BACK button to revert to the previous state of the page
and from the link from #reach4thelasers's answer, you have to set up a timer and check again and again the current anchor:
//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
setInterval("checkAnchor()", 300);
});
because there's no Javascript callback triggered when the BACK button is pressed and only the anchor is changed ...
--
by the way, the pattern you're talking about is now known as Single Page Interface !
You need to add an anchor to the URL whenever a change is made
www.site.com/page.html#anchor1
This will allow the browser to maintain the pages in its history. I implemented it in my current site after following this tutorial, which works great and gives you a good understanding of what you need to do:
http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/
Your example in the comments won't work, because it works like this:
Page Loaded
Page Changed, Add Anchor to URL (back button takes you back to back to 1)
Page Changed, Anchor Changed (back button button takes you back to 2)
Page Changed, Anchor Changed (back button button takes you back to 3)
.... and so on and so on..
If there is, it sounds like a pretty evil thing to do from a UX perspective. Why don't you design a "back" button into your application, and use design to make it obvious to the user that they should use your application's back button instead of the browser.
By "use design," I mean make your application look like a self-sufficient user interface inside of the browser, so the user's eye stays within your page, and not up on the browser chrome, when they are looking for controls to interact with your app.
You can do this with anchors, which is how it's done in a lot of flash applications, or other apps that don't go from page to page. Facebook uses this technique pretty liberally. Each time the user clicks on a link that should go in their history, change the anchor on the page.
So say my home page link is:
http://www.mysite.com/#homepage
For the link that works your javascript magic, do this:
My Other Page
This will send the user to http://www.mysite.com/#otherpage where clicking the back button will go back to http://www.mysite.com/#homepage. Then you just have to read the anchors with
window.location.hash
to figure out which page you're supposed to be on.
Take a look to this tutorial based on ItsNat a Java web framework focused on Single Page Interface web sites

IFrame Back button

I searched a lot to get rid of this problem on the internet but could not find a specific solution despite the problem being discussed in details previously.
The query is simple. My javascript dynamically adds an Iframe to the web page (which displays a feedback form). The problem is that, "after answering", now when the user clicks the back-button of the browser the iframe instead of the browser window is affected i.e. the questionnaire is displayed again. I want the browser back button to behave normally.
This behavior is really annoying and I am having real trouble fixing this.
I am using firefox.
Looking forward to the replies. Please inform me if I should give more details.
Thanks,
Your form has a submit button, which posts the page to the server. The back button will always send the user back to the form regardless of whether you use a iframe or not. The ideal way is to notify the user of a completed action, in this case thank the user for the feedback (using an alert box) and redirect the user to the home page or provide a button in the page saying "Back to Home".
Firefox and IE indeed act like you mentioned, but Chrome do not, and I'd guess other WebKit browsers would do the same.
In Chrome, clicking the Back button will land you where you want to go (the previous URL of the parent frame). i.e. Chrome to not add iframe URL changes in the back button history.
Sadly, I've found no way to force IE and FF to replicate this, so I used the AJAX post approach suggested above by Arun.
Here's my iframe source, which use jQuery to post the form, and replace the whole page with the result of that POST:
<form method="post" onsubmit="postForm(this);return false">
...
</form>
<script type="text/javascript">
function postForm(form) {
$.post(form.action, $(form).serialize(), postCompleted);
}
function postCompleted(data) {
$('html').html(data);
}
</script>
This works in all browsers; clicking the Back button will send you back to the previous URL a seen by the end user, instead of the initial form loaded dynamically in the iframe.
I encountered the same problem: I use a dynamically created iframe to show a "popup" on my page, whose SRC points to another page that has got a form and a submit button. After submitting that page, a JS callback is used to hide the iframe. As you explained, this causes a new entry to be added to the history (on IE at least).
But I found out that removing the iframe element from the DOM (instead of hiding it) results in the unwanted history entry being removed (tested on IE9)! Which is what the user would expect in that situation.
You can observe this yourself on IE9:
Open the back button menu (right-click the back button): you only have one entry for the current page
Press submit in the iframe => the back button menu shows one extra entry for the iframe
Remove the iframe from the DOM => the back button menu no longer shows that entry

Categories

Resources