How can I extract tokens from the current URL using JavaScript - javascript

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.

Related

Synonym for window.location.href in EL - Get current URL

I need to get current URL (in javascript window.location.href) for value of my <spring:param> (in context of creating back URL). For param's value I have to use expression language. Is that even possible, or What are you suggesting? Thank you for answers!
I need to get current URL (in javascript window.location.href)
It's available by the HttpServletRequest#getRequestURL().
So, in EL thus just ${pageContext.request.requestURL}.
It's available by the Read This.
So, in EL thus just ${pageContext.request.requestURL}.

replace current url without reloading the page

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

Parse URL value and remove anything after the last '/'

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

How to get an Id out of a Url client side?

If I have a URl like "/api/User/Details/2c021192-25cb-43e1-9bba-3bd5604a0a3d" what would be the best way to get the ID "2c02ds92-25cb-43e1-9bba-3bd5604a0a3d" out of the URL client side?
I need to be able to build a $.getJSON request with the ID and I'm looking for the cleanest way to do it using JavaScript, jQuery, etc. Thanks
$.getJSON('/api/User/2c021192-25cb-43e1-9bba-3bd5604a0a3d')...
Use regular expressions and extract the appropriate part (which seems to be UUID) from the URL.
The alternative is to just split the string by / and get last element.
EDIT: The example on how to retrieve UUID from the URI:
var url = '/api/User/2c021192-25cb-43e1-9bba-3bd5604a0a3d';
var pattern = /[\da-f]{8}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{12}/;
var match = url.match(pattern)[0];
url.match(pattern) returns array of matches, so assuming there is at least one, and only one match, you should pick it (using [0], as in the example).
Proof: http://jsfiddle.net/p6zud/2/
EDIT 2: Shortened the pattern used for matching (see revision history for comparison).
If the id is always going to be the last section of the URI, you could do something like
var url_array = document.location.split('/');
var id = url_array[url_array.length - 1];
using split, convert the url to an array of parameters. Then last array item in your case would be your ID.
there are very sophisticated URL parsers out there for javascript, perhaps you should look around on google for one that suits your needs.
The simpliest:
var id = '/api/User/2c021192-25cb-43e1-9bba-3bd5604a0a3d'.split('/User/')[1];
Gives you 2c021192-25cb-43e1-9bba-3bd5604a0a3d;
Try this
alert("/api/User/Details/2c021192-25cb-43e1-9bba-3bd5604a0a3d".split('/').slice(-1)[0]);

How to get everything before the hash from the address bar with Javascript?

I know I can get the hash value directly with this bit of code:
var hash = window.location.hash;
But is there anyway I can get the everything before the hash value directly as well?
Thank you!
Edit Using the Answer bellow: I am guessing the best way is..
var pageAddress = window.location.split('#')[0];
I use:
window.location.href.replace(window.location.hash, '');
you can try to subtract the has from the location
This took me 2s to Google:
http://davidwalsh.name/javascript-window-location
You want to take the href and split it on the '#', easy enough to do in JS.
This may or may not be of interest, if you don't mind jQuery (I didn't write it):
jQuery BBQ: Back Button & Query Library:
http://benalman.com/projects/jquery-bbq-plugin/
Just combine the text before the hash
window.location.origin + window.location.pathname + window.location.search

Categories

Resources