JavaScript Page Not Redirecting Properly - javascript

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();

Related

JS window.location.href not showing same value as in address bar

I need to read the url using some JS code in order to extract some parameters and execute some tasks depending on them. However I have two problems:
-The webpage is not mine so I cannot modify anything on it or inspect it in great detail. I just write some script that the client load from its webpage, and executes some functions on load. I can modify this script but not anything else.
-In this functions, I save in a variable the window.location.href value, but when doing some logs in order to verify what is reading, I see that the value does not correspond to the actual url in the address bar. Moreover, if I check the value of window.location.href in the browser console, then the value matches with the address bar!
I check this value at the start of my script and at the end and it does not match in any case. I don't know at which point of the loading does my script start execution, and I don't know what is causing this behaviour.
EDIT: Here is some code, I won't show it all as most of it works fine and doesn't have relation to my problem.
This is what I sent to my client to put in its webpage:
var s1 = document.createElement('script');
s1.src = 'source_of_my_script;
document.head.appendChild(s1);
s1.onload = function () {
s();
};
This is part of my script:
function s(){
var my_url_var = window.location.href;
console.log("1st value", my_url_var);
var param = extractParamFromUrl(my_url_var); //this functions get some value written in the url
switch (param){
case 'value_0':
// do things
case 'value_1':
// do other things
defalut:
// do default actions
}
console.log("final value", window.location.href);
}
I cannot show you the exact URLs because of privacy reasons, but they are similar to these ones:
Address bar url (and what i get if I check window.location.href in browser console): https://www.client_dummy_url.com/dummy/cobranded?origin=8000097& ... (more params separated by &)
What I get from window.location.href in script: https://www.client_dummy_url.com/drfd01/main/control.do?action=start&internet=S&app=EP& ... (more params, but not all the ones I want!)
Many thanks!
After some tries, I managed to get it to work with the solution posted by #Vaibhav at Access parent URL from iframe
//var my_url_var = window.location.href;
var my_url_var = (window.location != window.parent.location)
? document.referrer
: document.location.href;

JavaScript - delete all get parameters from URL in address bar without reloading the page

Is it possible to clear all GET parameters from the URL in the browsers address bar without initiating a page reload?
I tried to remove all GET prematers and then fake a page reload and immediatelly interrupt it as a workaround, but this does not replace the URL.
window.location.href = window.location.href.replace(window.location.search,'');
window.stop();
document.execCommand('Stop');
get the base url without parameters
baseUrl = window.location.href.split("?")[0];
then setting the url inside the address bar
window.history.pushState('name', '', baseUrl);

window.location not setting to new value correctly

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.

Javascript page refresh to base URL, stripping query string, on browser resize -

I'm using this piece of javascript code to refresh the browser when the user resizes it:
<script type="text/javascript">
$(window).bind('resize', function(e)
{
if (window.RT) clearTimeout(window.RT);
window.RT = setTimeout(function()
{
this.location.reload(true); /* false to get page from cache */
}, 200);
});
</script>
It works fine, except - the page I'm using has a query string with certain values, like:
http://www.example.com/index.php?w=123&h=456
What I'd like is the same functionality but to have the page refresh to the URL without the query string (or at least without its values).
Is that possible?
since location is an object ,you can change the property 'href' to whatever you like , for example you can change the new location and assign it to the href property:
your current location is : http://www.example.com/index.php?w=123&h=456
when you need to refresh the page without the query string, you can do this: window.location.href = "http://www.example.com/index.php"
hope helps,
good luck
Try using window.location = window.location.pathname instead.
If you to redirect the page to root domain, then use window.location.origin. If you want to include the script name too then use window.location.origin and window.location.pathname. So your redirect can be replace with
window.location = window.location.origin + window.location.pathname

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