Using Javascript, I'm trying to reload my page and add a parameter before doing so. I've added the alerts to debug why the parameter is not getting passed to the server:
alert('before: ' + window.location.href);
window.location.href = window.location.href + "?advanced_expand=true";
alert('after: ' + window.location.href);
window.location.reload();
What has me perplexed is that the first alert is:
before: http://localhost:3000/orgconf/quick_org_benefit/edit/784
and the second is:
after: http://localhost:3000/orgconf/quick_org_benefit/edit/784
without the parameter added...what gives?
Doing the window.location.reload() afterwards is pointless because setting window.location.href changes the page.
Likewise, window.location.href doesn't actually change until the page changes. That's why you aren't seeing the URL change.
If the page is reloading and it still hasn't changed, make sure your URL rewriting isn't removing GET parameters that come after a ?.
Related
I have a Sharepoint onprem server side webpart and I would need to replace my query parameter without refreshing the page so I used
if (history.pushState) {
var newurl = "<%=getURL()%>" + "/SitePages/searchresults.aspx?searchtext=" + searchText;
window.history.pushState({ path: newurl }, '', newurl);
}
now this would trigger a button click, and a postback would happen which refreshes the contents of my page without reloading it. it needs to get the query string parameter from the URL. its working when i use
HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
but I am not sure if this is the correct way to do it. and I noticed that in the browser network tab, the xhr call is still the old query parameter before i changed it via pushState.
Is there a way that I can change the URL and capture it in the code behind in asp?
I'm attempting to remove a url parameter status from the url but in the following alert, the parameter is still there.
var addressurl = location.href.replace(separator + "status=([^&]$|[^&]*)/i", "");
alert(addressurl);
location.href= addressurl;
How do i solve?
You are confusing regex with strings.
It should be:
var addressurl = location.href.replace(separator, '').replace(/status=([^&]$|[^&]*)/i", "");
Javascript context in web pages are to the page you are working on.
When you reload, redirect or move to any other page, javascript changes done in previous page will not be there. This has to be handled from server side.
Refresh repeats the last request to the server, which is going to ignore your javascript changes. Instead navigate to the new url with window.location = addressurl;
I'm redirecting the user from another page when they click the "Edit" button using the code below.
$('#editListButton').click(function(){
window.location.href = "http://localhost/yyy.php"; //redirect
// Changes that need to be made
$('#defaultText').remove();
$('#orderList').append('<p' + 'message' + '</p>');
});
The page redirects to the predefined link, after which I need update a html <div> tag with text. But the code coming from the other page does nothing. How can change the text in the div tag?
Pass it in the url.
$('#editListButton').click(function(){
window.location.href = "http://localhost/yyy.php?message"; //redirect
});
Then on the other page
var url = window.location.href.split('?');
var msg = url[1];
$('#defaultText').remove();
$('#orderList').append('<p>' + msg + '</p>');
Once you've triggered an operation that's going to reload the entire page, browsers (not all, but it seems like most) will just quit doing anything to the current page, essentially ignoring any DOM updates from the event loop.
To get around this, it generally works to delay the redirect with a short timeout:
setTimeout(function() {
window.location.href = "http://localhost/yyy.php";
}, 1);
Now the browser doesn't know that the page is to be reloaded, so it'll obey your DOM update request.
edit — If what you're expecting (and it's not really clear from the OP, but it's hinted at in a comment below) is that the code after updating the location should affect the new page, well that's just not how things work. You can put that code in the new page (and pass parameters via the URL you're loading, if necessary) and have it run there, or else you can change the architecture completely and load your new code via ajax.
There are many ways to pass information from one page to another. To give an idea of the concept, somewhat in relation to the question posted, here's one:
Page A:
$('#editListButton').click(function(){
window.location.href = "http://localhost/yyy.php?action=remove&value=" +
encodeURIComponent('ashdahjsgfgasfas');
});
Page B:
var action = /(?:\?|&)action=([^&$]+)/.exec(location.search)
if ( 'remove' === action[1] ) {
var value = /(?:\?|&)value=([^&$]+)/.exec(location.search)
$('#defaultText').remove();
$('#orderList').append('<p>' + value[1] + '</p>');
}
I have been having some trouble using a javascript script that automatically makes you get forwarded to use ssl. example,
http://www.example.com/link becomes https://www.example.com/link
But my issue is that is continuously loads the script, but I want it to stop when it is already loaded. It reloads continuously, making it very annoying and hard to click the links on the page.
Here is my current script
window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;
You need this:
if(window.location.protocol == 'http:') {
window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;
}
...or even better:
if(window.location.protocol == 'http:') {
window.location.replace(window.location.toString().replace(/^http:/, 'https:'));
}
Second variant is better because:
URL may be complex and will be handled properly
Because of using window.location.replace() instead of directly assigning a string to window.location, previous URL will be removed from history and when user clicks 'back' button, he will jump to original page, not to the one with "http:" protocol: http://www.w3schools.com/jsref/met_loc_replace.asp
But it's better to implement this on server side.
I have a seemingly simple question, but can't find the answer. I have a webpage, which may have resulted from a POST request and may have an anchor (#) in the URL. I want to reload this page as a GET request in JavaScript. So it's similar to this question, but I actually want to avoid the POST, not just the warning about it.
So, for example, if the page resulted from a POST request to "http://server/do/some?thing#" I want to reload the URL "http://server/do/some?thing" as a GET. If I try
window.location.reload(true);
that causes IE to try a POST. If I instead do:
window.location = window.location.href;
this does nothing when the URL has an anchor. Do I really need to do string manipulation myself to get rid of the "#whatever" or is there an easier, "better" way to do this?
The best I've come up with so far is:
function reloadAsGet()
{
var loc = window.location;
window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
}
Try the following:
location.replace(location.href)
You can try this
location=location.href