window.location not setting to new value correctly - javascript

I am currently having issues with a bit of javascript I am running on a website. I am trying to redirect to a new page however instead of setting the redirect url to the page I pass in it seems to concatenate it to the end of the existing url.
For example if I am current on www.stackoverflow.com/questions and want to go to www.stackoverflow.com/questions/ask the url becomes www.stackoverflow.com/questions/www.stackoverflow.com/questions/ask
When I output the variable containing the URL to console it appears correctly
var nextPage = window.location.hostname + data.redirectUrl + "?confirmationtype=success";
console.log(nextPage);
window.location.href = nextPage;
the above output would be www.stackoverflow.com.au/questions/ask but would redirect to www.stackoverflow.com/questions/www.stackoverflow.com/questions/ask
I have done some searching and in many cases it seems to be related to onpost methods on the form, but I cant see how this would be an issue on my page and I am using a polling timer method to trigger these calls so page posts should not be a factor. Also if I try onpost="return false" other pages that use the same layout fail to post :S
Any help would be appreciated.

Url without protocol ("http:") becomes relative Url - so when you try to set window.location it will not perform navigation.
Something similar to:
var nextPage = window.location.hostname + data.redirectUrl + "?confirmationtype=success";
console.log(nextPage);
window.location = "http://" + nextPage;
You can use window.location.protocol instead "http://", you can use window.location or windows.location.href in code above.
Note that your sample shows some strange window.location.href(nextPage); which is function call, but href is not a function.

Related

How to refresh a page in JavaScript without POSTing?

I've seen this question already, but the top answers all suggest window.location.reload. I've just discovered that this will actually do a POST if your page was loaded with a POST.
I reckon I could do window.location.href=window.location.href but that won't work if there's a hashtag in the URL I'm told.
So how can I get the browser to perform a GET on the current page, including query params (with or without the hash)?
You can manually construct your URL:
window.location.href=window.location.origin + window.location.pathname + window.location.hash;
function refresh() {
window.location.href = window.location.pathname + window.location.search;
};
You don't need the origin (it doesn't work in old IE anyway). You should add .search if you want to keep the query params. Don't add .hash because it won't refresh if there is one.

JavaScript Page Not Redirecting Properly

I'm trying to create a page system for user_profile.html using a parameter from the URL and accessing using JavaScript.
However, for some reason my page does not refresh using the # parameter. I'm not sure why this is happening. I've put my redirection function and the output below.
Code:
function pageRedirect(page) {
var url = window.location.href.replace('#'+window.location.hash.substr(1), '#'+page);
console.log("####");
console.log(url);
console.log("####");
window.location.href = url;
}
Chrome console output:
####
user_profile.js:296 http://**********/user_profile.html#1
user_profile.js:297 ####
Navigated to http://**********/user_profile.html
Even though the URL gets changed to the same thing with a #1 at the end it does not get refreshed with that parameter.
The simplest way to change the hash of an url will be :
window.location.hash = page; // not the URL, just the '#something' part without the '#'
And if you need to reload the page after this, you can do this :
window.location.reload();

Custom Social Sharing: shareUrl passing to URL but not actually sharing the shareURL

I have five custom share icons for Facebook, Twitter, Google Plus, Pinterest and Email. For each, on click, I would like them to share the current URL. The problem is that, while the shareURL is getting passed to the window URL (such as: https://twitter.com/share?url=shareURL), this shareURL is NOT getting passed into the dialogue box for each social medium. So even thought it appears in the browser URL box, the individual social users aren't actually sharing anything. The dialogue box appears empty.
In my example above, shareURL = thisURL in my script below. Here is a sample of my script:
function share_twitter(thisURL){
window.open("https://twitter.com/share?url=" + encodeURIComponent(thisURL));
}
$(document).ready(function(){
thisURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
$(".twitter").click(function(){
share_twitter(thisURL);
});
My hunch is that this has to do with global variables and the document ready function. Not sure where to go from here.
the arguments is not complete to redirect command, need variable %(cmd) to finish it up..
ex: https://twitter.com/share?url=%{url} , & make sure it has right url path check. try...

Window.Location Refreshes instead of Redirects

I have a JQUERY function as follows
this.getURL = function()
{
var name = getName();
alert("Menu.aspx?name"+name);
//window.location = "Menu.aspx?name"+name;
}
When I alert the URL I am attempting to go to, it is correct. However, when I call window.location on that string, the page just refreshes without going anywhere.
I have similar code where I have used window.location and it works. I typed in the url into my browser and it works as well.
At worst (even if the URL was wrong), I was hoping that it would just redirect me to some URL. However, I can't get it to do anything other than refresh the current page.
Also to clarify, the page which calls this function is not Menu.aspx
Thanks in advance.
If you're using a relative path try setting window.location.pathname, otherwise set window.location.href for a full path.
You may also want to try self.location.href
In my experience, it's been difficult to get redirects like this to work right. I've had to use window.location.replace(<url>). If you're just changing an anchor tag, it's even more difficult. You have to do the following to get it to work in all browsers:
window.location.replace(<url>);
window.location=<url>;
window.open(<url>,'_self');
window.location.reload();

Sending parameters via url during reload

I am doing an ajax request and in the success callback I want to refresh the page to reflect the changes. I am trying to pass parameters to the same page as I refresh that page.
The url is something like:
http://localhost:8080/details#component/12 to which I am appending the parameter as said below.
window.location.href += "?code=approved";
window.location.reload();
The above works in Firefox but not in IE, can you please help here?
Chris.
Try using these for IE:
window.location.reload(false); // To get page from cache
window.location.reload(true); // To get page from server
The hash is the problem; you append your data to the URL fragment (part after the #) The fragment isn't send to the server, ie the url doesn't change so no need to request the page again. Try manually adding '#some_text' to the url in your browser and see what happens ;)
Try something like this:
var newloc = document.location.href;
if(document.location.hash){
// remove fragment
newloc = newloc.substr(0, newloc.indexOf(document.location.hash));
}
newloc += document.location.search ? ";" : "?"; // prevent double question mark
newloc += 'code=approved';
// append fragment back to url
if window.location.hash is empty, you cant assign to location.href a new value without using a correct function (at least tested in chrome).
try the window.location.replace:
if (!window.location.hash)
{
window.location.replace(window.location.href + "?code=approved")
}

Categories

Resources