I want to change pathname via function window.location.pathname. I have got this source code.
var hash = window.location.hash;
window.location.pathname = hash;
in Mozilla it works right, but in Chrome doesn't. Chrome write me this adress.
/%23!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783?stranka=kontakty#!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783
Value of hash is #!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783
Have someone any idea?
Thanks.
You'll have to understand that location.hash includes the # itself as well. The rest of the location.hash is, per spec, URLencoded, but the # isn't.
I said "per spec", as Firefox has a bug related to the location.hash property.
If you want to change location.pathname to the hash with the value included, you'll have to encode the # before doing so.
Example:
var hash=location.hash.substring(1)
location.pathname='%23'+hash
If you don't want the hash to be included, just use
var hash=location.hash.substring(1)
location.pathname=hash
Related
I am trying to use jQuery to get the current full URL of a page. The URL of a page looks like this:
http://myurl.com/ebook?page=43
If I use window.location.pathname it only returns everything before the ? so I would just get
http://myurl.com/ebook
How can I get the entire URL?
You have:
window.location.href
and also:
document.URL
If you want to return the whole URL, you need to use window.location.href.
Using window.location.pathname only returns the path portion of the URL which excludes the query parameters and fragment (i.e., starting at the / following the host but only up to a ? or #).
Example:
> window.location.pathname
"/ebook"
> window.location.href
"http://example.com/ebook?page=43"
Also, see the documentation for window.location for more available properties.
I want to take the current URL from browser and replace the last number param by other number, my example URL:
app#!folder/1/document?p=1
the param to replace - ?p=1 into ?p=3
the param number is generated dynamically so I have to take the param from URL
I want to execute this JavaScript as String,
thanks a lot for any help
I reslove the problem(VAADIN FRAMEWROK) without using javascript
String url = Page.getCurrent().getUriFragment().toString();
url = url.replaceAll("\\?p=([0-9])?", "?p=1");
Page.getCurrent().setUriFragment(url, true);
Unless I'm missing something, just use String#replace() like so
url = url.replace("?p=1", "?p=3");
Note that a modification to the current URL will reload the page unless it happens in the hash part (after the # character), which is obviously what you're trying to do.
The current URL can be modified in javascript via the location.href variable:
location.href = location.href.replace("p=1", "p=3");
My website URL is (example) http://mypage.com/en/?site=main. Then comes JavaScript code that, together with PHP, parses the data.
After that, I need some code that will change the URL inside the adress bar to http://mypage.com/en/, that is, removes the stuff after the last / (slash).
If possible, it is should be jQuery/JavaScript code.
I found something that will work.
You have to use a method called replaceState().
Mozilla developer reference
var str = window.location.href;
window.history.replaceState({}, '', str.substr(0,str.lastIndexOf("/"))+"/");
Use split() function in javascript.
Example :
var url = "http://mypage.com/en/?site=main";
alert(url.split('?')[0]);
Could you solve my question please
window.location.search.substring is not working in IE 8
Regards
Ravindran
Maybe related:
Internet explorer (v9 at least, which is what I'm testing here) doesn't fill location.search when there's a hash (#) in front, and packs everything into location.hash instead.
Here's my workaround:
var query = window.location.search.substring(1);
if (!query) {
var hash = window.location.hash;
query = hash.slice(hash.indexOf('?') + 1);
}
I read that:
Location objects have a toString method returning the current URL. You can also assign a string to window.location. This means that you can work with window.location as if it were a string in most cases. Sometimes, for example when you need to call a String method on it, you have to explicitly call toString
https://developer.mozilla.org/en/DOM/window.location
So I'm thinking:
window.location.search.toString().substring(...) ?
It'll work on Internet Explorer if you change it to document.location.search.substr
Given an URL such as /abc.html#variable1, I want to capture the variable1 part to determine a given user's "virtual page" when working with JavaScript (jQuery).
If I understand your question, you don't need at all jquery, just use
to get the url use
var the_link = document.location
to get just the hash part of the url (the text after #) use
var the_hash = document.location.hash
to get the GET variables you can use
var get_vars = document.location.search
Examine the window.location.hash. Not jQuery specific.
I'm not sure if this is what you are asking for, but the way to retrieve the page's url is:
var pathname = window.location.pathname;
It is really javascript, no jQuery involved.
If you want to addd some jQuery:
$(document).ready(function() {
var pathname = window.location.pathname;
});
Hope it helps.
You can use window.location.hash to get the value after the # sign.
You can find some information on the topic here. And within the context of unique URLs for AJAX, you can check out this extensive article.