Remove parameter from query string without reloading - javascript

Is it possible to remove parameter from query-string with js, I have tried below one unfortunately its works in browser console not in JS file.
var clean_uri = location.protocol + "//" + location.host+location.pathname;
window.history.replaceState({}, document.title, clean_uri);
what would be the best way to do it.

The reason this would only work in the browser is that the window.history API is only available in your browser. I'm not quite sure what you mean by 'from file'. But as one of the commenters pointed out the problem is in the context of where it is executed. If for instance you are executing this in a Node environment rather than a browser, there is no window.history api. Node doesn't even have a window object.
Since you've added an AngularJs flag, it might be that the routing inside the app is listening to changes to the URL and will act on them. Angular may have it's own way of allowing you to change the URL but I'm not familiar enough to help with that.

Related

How to use window.open(url,"_blank") without changing calling application's url?

This is my js window.open() code.
var url = "https://www.google.com"
window.open(url, "_blank", "menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes,width=640,height=480", false);
It opens a new browser window with the "url" as expected.
But the issue is that, it also changes the current url in my main web application.
For example before my window.open() call if I was on this url:
https://example.com/#!/projects/56asda/view
After the call the url redirects to this:
https://example.com/#!/
How can this issue be prevented? I have not found any solution for this on the internet.
I am using angularjs 1.0 for my frontend.
Please refrain from answering that "_blank" should work etc.
Any help will be invaluable.
I have tried to duplicate your issue but I have found no issues while executing the code which would mean your issue might be related to your web server settings.
Anyways, have you tried calling it like this:
var url = "https://yourlink.here";
window.open(url, "_blank");
Without anything like "menubar=no" or other arguments?
If you haven't, I suggest you do and if that works, just add the arguments one by one to check which one contains the error.

What is the difference between window.location and $location.path?

In MVC angularJS application, how can I redirect to MVC page.
I tried below two options
First
// It doesn't work
$location.path("/MyPage1");
Second
//It works
window.location = "/MyPage1";
Please suggest best way to redirect and why ?
REMEMBER : I am not using angularJs Routing.
Comparing $location to window.location official doc clearly stated
see the section at this location
seamless integration with HTML5 API
window.location: no
$location: yes (with a fallback for legacy browsers)
and more
Both do have their own merits. They are clearly described in the official docs as mentioned by #Mohammad. So depending on the circumstances choose any of the either :
Use $location : When you do not require a full page reload when the browser URL is changed, wants to avail the angular internal life-cycle benefits and where you don't need to support old legacy browsers.This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.
Use native window location : When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: window.location.href or when you want to work with row level object properties that you can directly modified. i.e like Force reload window.location.reload().

Custom dojo widgets not loading on default.htm

I've got a web page (default.htm) that loads some custom dojo widgets. The widgets load fine when the entire url is typed:
http:/www.eg/default.htm
but when the site is hit as:
http:/www.eg
the widgets dont load.
when they load properly (when default.htm is specified) the console message is:
XHR finished loading:
GET "http://www.eg/Templates/WatershedMap.htm"
when they dont load the console message is:
OPTIONS http://templates/WatershedMap.htm net::ERR_NAME_NOT_RESOLVED
I'm running iis 7. Does anyone have an idea of how I might fix this?
Thanks
I suspect that in your dojoConf or data-dojo-conf, you are using location.pathname, is that correct? Or perhaps directly in your xhr request where WatershedMap.htm is loaded?
When you view the page with just http;//www.eg/, the location.pathname is just a slash "/". If then, for example, xhr tries to do this:
xhr(location.pathname + "/Templates/WatershedMap.html")...
... then the request will actually go to //Templates/WatershedMap.html.
That double slash means "protocol relative url". The browser will take the same protocol scheme (http/https) as the current page, and append whatever comes after the double slash.
In other words, that will actually try to make a cross domain request to http;//Templates , which triggers a preflight OPTIONS request.
However, when your page is loaded with http;//www.eg/foo/, the location.pathname will be "/something/something", and the request will go to http;//www.eg/foo/Templates/WatershedMap.htm.
You will have to share some more code if you need help to pinpoint the problem. Look through your code for location.pathname and see if you find anything that may be relevant.
Edit: based on your comment, your dojoConf has the following:
packages: [{
name: "Templates",
location: location.pathname.replace(/\/[^/]+$/, "") + "/Templates"
}]
The line with replace() in it takes the current page's path (for example /foo/bar.htm), and removes the last slash and everything after it, then appends "/Templates".
It is supposed to ensure that whenever you load something that starts with "Templates" (for example if you do dojo/text!Templates/Map.htm, it will look in the same directory on your server as the current page.
However, When you are on http;//www.eg/ , the pathname is simply a slash, and nothing is removed. So you end up with "//Templates". As mentioned earlier, this becomes a protocol relative url, with Templates as the hostname. Definitely not what you want!
On the other hand, when you are on http;//www.eg/default.htm, the pathname is /default.htm, so all of that is stripped away, and you're left with just "/Templates". This is what you want!
You could solve it by simply replacing the line with:
location: location.pathname.replace(/\/[^/]*$/, "") + "/Templates"
Only a single character difference (+ became *)! Now it will remove the single slash if you are viewing http;//www.eg/ as well.
In my opinion though, it's better to use an explicit path. If you know that /Templates will always be http;//www.eg/Templates, you may as well do:
location: "/Templates"

Force (or ask nicely) to refresh the browser

So I run a site that uses a lot of javascript and ajax. I understand how to make users refresh their browser when the browser loads. But what happens if I need them to refresh their browser after they have loaded the site?
I want to change the ajax that is served to the client to speed up things up, but this is going to cause errors for the users who have not yet refreshed their browser.
The only solution I can come up with is that when a new version of the JavaScript file is required, the site uses a popup that asks the users to force refresh their browsers. (This won't really fix the current version, but would prevent future issues.)
I hate to use a popup for something that I could do automatically. Is there a better way to force updates for the client?
window.location.href = "http://example.com"
replaces the current page with the one pointed to by http://example.com.
You sound like you are having trouble with your JavaScript getting an updated version of the data it loads through Ajax methods, is that correct? For instance, if two Ajax calls try to load 'data.txt', then the second call merely uses the cached version.
You also may be having trouble with loading new versions your script itself.
The way around both of these problems is to add a randomly-generated query string to your script source and your Ajax source.
For example, make one script that loads your main script, like this:
/* loader1.js */
document.write('<script src="mainjavascript.js?.rand=', Math.random(), '"></script>');
And in your HTML, just do
<script src="loader1.js"></script>
The same method works for JavaScript Ajax requests as well. Assuming that "client" is a new XMLHttpRequest() object, and has been properly set up with a readystatechange function and so on, then the you simply append the same query string, like this:
request = client.open('GET', 'data.txt?.rand=' + Math.random(), true);
request.send();
You may be using a library to do your Ajax requests, and so it's even easier then. Just specify the data URL as 'data.txt?.rand=' + Math.random() instead of merely 'data.txt'

javascript: will window.location make any warnings?

If I use window.location = ""; to redirect a user, will any browsers give the user a warning message (since the page is redirecting without the user's consent)?
Should I use window.location = "" or window.location.href = ""?
No you can redirect the window location on your own. The only similarly related thing you can't do automatically is click a link for the user with an event.
You might be thinking of this type of notice:
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}
No. You will not get any warning (like you do if you close the window). And I have never seen any difference in location and location.href, but I use the last :)
AFAIK window.location and window.location.href should be pretty much eqivalent.
It's been a while since I've used it, but I don't remember ever seeing a prompt before leaving the page. Most of the time when I have been prompted it's been because I explicitly put a confirmation in, such as when clicking a link to delete something.
Setting window.location shouldn't cause any issues with redirection. However, if there is a hash, then some browsers may deal with it differently.
From the MDN spec:
Note: The example above works in situations where window.location.hash does not need to be retained. However, in Gecko-based browsers, setting window.location.pathname in this manner will erase any information in window.location.hash, whereas in WebKit (and possibly other browsers), setting the pathname will not alter the the hash. If you need to change pathname but keep the hash as is, use the replace() method instead, which should work consistently across browsers.
There shouldn't be any difference between location and location.href since whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.
Source: https://developer.mozilla.org/en/DOM/window.location

Categories

Resources