selector back button history web - javascript

Would need to know the selector button to return to the previous page, for jquery or javascript.
to be understood would be something like:
('back-button').click(function(){
});
I want this and I need that when you click back to return to the home of my web.
Thank you very much

HTML:
<button id="back-button">Go Back</button>
jQuery:
$('#back-button').click(function(){
window.history.back();//go back one page
});
Other possibilities:
window.location.history.go(-1);//go back one page
window.history.back();//go back one page
window.location.href = 'http://www.google.com'; //go to google.com

It's about as clear as mud, but I think he is either after this:
('#back-button').click(function(){
location.href = "/urlofyourhomepage/";
});
Or this:
('#back-button').click(function(){
window.history.back()
});
With a button being on the page with an ID of back-button.
Go Back
I don't (or at least I hope not) think that he wanted to intercept the back button functionality of the browser.

Related

How to redirect user to current url

I need to redirect user to the same url of the page.
I know that window.location.href = 'custom_url'; will redirect to a custom_url.
But I need to redirect him, to the same page that he's already in.
So how to do that with jQuery?
So you need to refresh current page:
location.reload();
console.log("loaded");
<button onclick="location.reload();">Refresh Page </button>
Or by click via jQuery:
$('#something').click(function() {
location.reload();
});
the thing you could do is using
window.location.href = window.location.href;
it may take some time more than usual to read and change the href.
you can also use
location.replace(window.location.href);
using this, when you check your history,
you will not see the site reloaded many times (I am using chrome).
and you can also use
location.assign("/");
.

jQuery - change attribute of a page after being redirected to it

So I'm making a wordpress site, and as such, I want to keep my custom javascript to a minimum.
Basically, I have Page1 and Page2, each with several child pages. In the Page1-children, there are links to the Page2-children. The Page2-children have a "Back" button in them that currently just navigate to Page2. What I'm trying to accomplish is: if the Page2-child was navigated to from a Page1-child, I'd like to set the href of the "Back" button to the page it was navigated from. Here's what I've got so far:
jQuery(document).ready(function($){
$('.application-button').on('click', function(event){
event.preventDefault();
var currURL = window.location;
window.location.href = event.currentTarget.href;
$('.dynamic-link').attr('href', currURL); //<-- .dynamic-link is a class of the Back button
//I figured that if it came after the window.location change, it would still be called
});
});
I'm sorry if the explanation before the code is hard to follow, but I can't really explain it in too much more detail, as it is somewhat work-sensitive.
Any and all help is appreciated!
A good way is to navigate to the last page in javascript
<script>
function goBack() {
window.history.back()
}
</script>
<body>
<button onclick="goBack()">Go Back</button>
</body>
http://www.w3schools.com/jsref/met_his_back.asp
In your case :
$('.application-button').on('click', function(event){window.history.back();});

Back button rewrite to redirect back to new location

I have a situation where I want to rewrite the back history
for example I am at my page:
http://mysite.example.com
and I go to another page by clicking a link
http://mysite.example.com/sports
Now when I click back, I want to be able to have a parameter appended to the URL
so by click back the users will actually need to go here
http://mysite.example.com?backfrom=sports
Now I know that html5 has apis that I can use but for some reason its not for me
on my http://mysite.example.com/sports JavaScript I add this
<script>
history.replaceState(null, null, "?backfrom=sports");
</script>
but that just change the url and when I click the back button, I still end up at
http://mysite.example.com
What can I do to change the history in a way that when I click back button it includes the parameters.
I think there is onpopstate but I am not sure how can I make use of it.
I think of a possible solution which could be to add the script in the index page of http://mysite.example.com like this
<script>
$('body').on('click','.mylink', changehistory);
function changehistory(){
history.pushState(null,null,'?backfrom=sports');
}
</script>
this will fake the URL and now when I click back from
http://mysite.example.com/sports
I get back to
http://mysite.example.com?backfrom=sports
Thought I would share

Changing shown URL after GET Request

Currently I have 2 pages based on the following URL MyNewApp/purchase and MyNewApp/confirm. On the 2nd page that is /confirm I added a back button with the following
HTML
<button type="submit" class="btn" id="back">
Go Back
</button>
JS
$('button[id="back"]').click(function() {
this.form.action="backToPurchase";
this.form.method="GET";
});
After pressing the back button everything works fine except the URL changed to /MyNewApp/backToPurchase?_method=. How can I set the URL to /MyNewApp/purchase after pressing the back button ?
You Can't just change the URL as you wish.
Think about it, if you could change the URL like that it would be a big security problem.
BUT try to use POST instead, or just send the user to a new page so the URL will change, You have lots of options to solve it, just think.
This is technically the answer to your question, though you might want to think about a better way to do what you want.
window.history.pushState(null, null, “/new-url”);
I would recommend a better option, instead of submitting a form just for redirection, use the following:
$('#back').click(function() {// <-- selecting by id is much faster than selecting by attribute "id"
location.href = '/backToPurchase'; // or '/purchase';
// OR you can do the following :
history.back(); //this will performs as you clicked the back btn from the browser
});

Make browser go to different url on button click?

I've got a jquery-ui button. When clicked, I want to move the browser to a url within my domain:
<button onclick='foo'>Go</button>
function foo() {
window.location('/otherpage');
}
is that the right way to do it, and address it relatively? Is there a better way to do this?
Thanks
$('#button-id').click(function() { window.location.pathname = '/otherpage'; });
What do you mean by relatively? To the current page or to the current host? There are a few options you are able to use.
It should be window.location = '/otherpage'; Other than that I think it's fine.

Categories

Resources