Sending parameters via url during reload - javascript

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")
}

Related

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/jQuery hash URL doesn't execute

I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
$(function($){
var key=$(location).attr('hash');
if (key){
$('#div').find('div.div2').load('sig.php?k='+key)
}
});
This works fine when I type in the URL manually or refresh the page but whenever I use <a href="url#SecReTkEy"> or Javascript/jQuery functions such as:
$(location).attr('href','url#SecReTkEy');
$('a').prop('href','url#SecReTkEy');
window.location.replace('url#SecReTkEy');
window.location.href('url#SecReTkEy');
Can any one tell me why and how to fix this?
`
you should use
location.href = #whatever
You don't need JQuery in order to retrieve the hash. Use the fully qualified reference to window.location.hash (I'm guessing location didn't work as you already have a var named location).
Also, don't forget to URL encode the key using encodeURIComponent. Additionally the $ is not needed if you want to execute this on document ready.
$(function(){
var key = window.location.hash;
if (key){
$('#div').find('div.div2').load('sig.php?k=' + encodeURIComponent(key))
}
});
I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
This was due to it only launching once rather than having an eventBinder on hashchange.

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

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

how to refresh web page using javascript?

I want to refresh a web page using javascript and, I knew how to do it but I have one problem,
before refresh:
url: http://www.example.com/index.html#introduction
after refreshed:
url: http://www.example.com/index.html
I want to use that hash as parameter in another javascript function after page has refreshed.
So, how can I save that hash after the page is reloaded?
I belive you have implemented refresh using location.reload() method, instead try redirecting to the same page using location.href like below.
location.href = "http://example.com/index.html#introduction"
This will refresh the page and also maintains # in url.
As commented, if u use only static code then u need to use cookies(if also support legacy browsers) else u can use localstorage(for modern browsers) for storing the hashValue.
to reload without hashvalue use the following code
window.location.href = window.location.href.split("#")[0];
before calling the above code u need to save the hashValue.
var hash = location.hash.substr(1);
localStorage["hashValue"] = hash;
retrive the above value later as
var hash = localStorage["hashValue"];
Try
escape(window.location.href)
if you want to add parameters:
for example
escape(window.location.href="parameter="+param);
try this
window.location.href = window.location

Categories

Resources