How to redirect user to current url - javascript

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("/");
.

Related

Href to Onclick redirect

I have research this topic through the community, although I cannot find an answer. I am using Bronto's direct add feature (attempting to use it), The documentation isn't that great.
In summary, the href link subscribes the user on the email list. The only problem is that this link opens a new page. When I want the user to stay on the same page. I though about doing a redirect, when clicking the link, though I am not sure if that would work.
I have tried this:
//Html
<a id="subscription" href="http://example.com">Subscribe</a>
// Jquery
$("#emailsubscribe").click(function(e){
e.preventDefault();//this will prevent the link trying to navigate to another page
//do the update
var href = "http://example.com";
//when update has finished, navigate to the other page
window.location = "href";
});
The goal is that I am trying to make it where the user clicks on the link, it subscribes them to the email list, but immediately redirects them back, without opening another window.
You're looking for AJAX. This allows you to make requests without actually navigating to a page. Since you're using jQuery
$("#emailSubscribe").click(function (e) {
e.preventDefault();
$.get("http://www.myurl.com", function (data) {
//All done!
});
});
You have 3 options:
Do an AJAX request to subscribe the user
$('#emailsubscribe').on('click', function () {
$.get('/email-subscribe/' + USER_ID, function () {
// redirect goes here
window.location = 'REDIRECT_URL';
});
});
Use an iframe and when the iframe has loaded close the iframe (ugly, hacky, not recommended)
Have the subscribe page redirect the user back. Aka do the common messages of "You have been subscribed. Redirecting back in 5seconds...". You would need to pass the redirect link to the subscribe page, aka
window.location = '/subscribe/USER_ID?redirect_to=/my-redirect-page'
You need to refer the var, instead of typing another string to redirect.
//Html
<a id="subscription" href="http://example.com">Subscribe</a>
// Jquery
$("#emailsubscribe").click(function(e){
e.preventDefault();//this will prevent the link trying to navigate to another page
//do the update
var href = "http://example.com";
//when update has finished, navigate to the other page
window.location = href; //<<<< change this.
});

window.location not refreshing to the existing URL

I have a PHP page with implementation of jQuery horizontal tabs. I have added a jQuery script so that the page URL reflects the tab#. I have a form on the page and upon form submission I need to refresh and stay on this same page to include the jQuery correct tab number.
This is the code to add the jQuery tab number to the URL:
<script>
jQuery(function($) {
$("<p>").html("Loaded at " + new Date()).appendTo(
document.body
);
showTab(location.hash || "#tabs-1");
$("#nav a").click(function() {
var hash = this.getAttribute("href");
if (hash.substring(0, 1) === "#") {
hash = hash.substring(1);
}
location.hash = hash;
showTab(hash);
return false;
});
function showTab(hash) {
$("div.tab").hide();
$("#tab-" + hash).show();
}
});
</script>
The full URL of the page is http://somedomain.com/includes/nonadmin_user_profile.php#tabs-5 and includes the tab number for the jQuery horizontal tab.
I am using this script to refresh and stay on the same page: echo "<script>window.location=window.location</script>";
On refresh here is the problem as it lands at this URL which does not include the tab number. http://somedomain.com/includes/nonadmin_user_profile.php
Any suggestions would be appreciated very much.
Here is another detail: The problem described above does not occur if I merely refresh the page with the browser refresh button or if I right click the page and refresh. In this instance the page refreshes and stays on the full url with the tab#.
Per Disaster Faster's request, the issue encountered was simply that the browser was not going to the desired location of the page. The form data was successfully submitted and correct page was loaded.
The solution to his issue was modifying the form's action attribute to include the location information (similar to adding location information to an anchor).
Original:
<form action="nonadmin_user_profile.php" method="post">
New:
<form action="nonadmin_user_profile.php#tabs-5" method="post">
Original Post:
The window.location = window.location redirect should include the location information.
JSFiddle: http://jsfiddle.net/6dqcmh9d/
If you click the button first, it'll report the URL with the location information because it hasn't been added to the URL. Then click the link and re-click the button. You'll receive the URL with the location information added.
If you want just the location information, you can use window.location.hash, but this will only produce the location on the page, not the URL of the page.
The problem you'll run into will be with the form submission. To submit a form without changing the page, you'll either have to submit the form to a new window (add target="_blank" to the form) or implement AJAX. In either case, we'd need a little more of your code to help with integrating it properly.
It is likely that the tab controls are being handled with onclick events or something similar, and are not listening for hash changes onload. You'll likely need to add some custom JS to force the tab change if there's a hash change.
What's wrong with using the reload() method?
document.location.reload(true);
From the docs:
The Location.reload() method Reloads the resource from the current
URL. Its optional unique parameter is a Boolean, which, when it is
true, causes the page to always be reloaded from the server. If it is
false or not specified, the browser may reload the page from its
cache.
If you need to integrate it into a PHP echo struct, use:
echo '<script>document.location.reload(true);</script>';
You should use reload() to refresh the page, eg:
window.location.reload();
Or given your example:
echo "<script>window.location.reload();</script>";

how can I navigate to page but ensure that the target page will be refreshed

I have a Login.html page from which I'm navigating to my Main.html
now when I;m doing a logout I want to navigate back to the login.html but I also want to ensure this page will be refreshed.
I don't want to disable the page cache, I just want only in this specific scenario to navigate it after refresh.
I tried the following:
window.location.replace but it doesn't refresh the page.
window.location.href - also doesn't refresh the page.
window.location.reload() - Refresh only the current page.
#Christof13 suggestion regarding passing a parameter is the only way I can see but it loading the page twice and it's very ugly,
any other suggestions?
How about setting a Timeout on the logout event? This way, the location reloads only once.
$("#myLogOutButtonOrLink").click(function() {
setTimeout(function(){
window.location.reload();
}, 1000);
});
My solution was using a global variable on the window scope as the following:
on main.html:
window.globals.RefreshRequired = true
on Login.html:
if (window.globals && window.globals.RefreshRequired) {
window.location.reload();
}
In this way the refresh will be done if I already visited main.html during the current browser instance.

window.location.replace(document.URL) not triggering if url contains '#?' at the end

In my sample website, I was trying to redirect my page using javascript window.location.replace method. As I need to refresh the page, I used document.URL. It works well. But sometimes I noticed that it not working without giving any error. Finally I found that some links adds # in address bar and one of my javascript add a ? to the same. At that time the code window.location.replace(document.URL) wont execute. Is that due to the characters in the URL?
here is my function(sample)
function sample() {
alert(document.URL);
window.location.replace(document.URL);
}
the alert showing url like http://localhost/something/#?. and at this time it will not refresh the page.
Use any of these
location.reload();
window.location.reload();
window.location.href=window.location.href
You need to use location.reload(); instead.

selector back button history web

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.

Categories

Resources