This question already has answers here:
How to get URL parameter using jQuery or plain JavaScript?
(34 answers)
Closed 4 years ago.
I have this url
https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp
I need to get text of table name from the url when page loads.
I tried this
location.href.split('&')[1]
It returns "table=user". but I need the table name alone as user.
How can I get this?
Try using URL.searchParams
// location.href
let str = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp";
console.log((new URL(str)).searchParams.get("table"));
Split it once more:
location.href.split('&')[1].split('=')[1]
Or you could use Url class (no support in IE):
var url = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp"; //window.location.href
var url = new URL(url);
var table = url.searchParams.get("table");
console.log(table);
location.href.split('table=')[1].split('&')[0]
Don't assume the parameter order ;)
Related
This question already has answers here:
Last segment of URL with JavaScript
(30 answers)
Closed 2 years ago.
i have the following link:
http://tili-click.startboomservice.com/tracking/adClick?d=IAAAAAAgAAA6X
I need to get the query string with before question mark
so the result would be
adClick
this must done on many link with similar pattern.
I couldnt find solution like url encode that i use for host and pathname
check window.location you will get idea, there are more properties that will help you. e.g. some of them are:
window.location.href
window.location.hostname
window.location.search
window.location.pathname
You can do something like:
var queryString = window.location.search;
var paths = window.location.pathname.split("/")
var lastPath = paths[paths.length-1]
var rsult = lastPath+queryString;
You can use URL.pathname() and .split() string method:
const url = new URL("http://tili-click.startboomservice.com/tracking/adClick?d=IAAAAAAgAAA6X");
const pathname = url.pathname.split('/');
console.log(pathname.pop());
This question already has answers here:
how can I remove this URL section from this string (representing the URL) using old JavaScript?
(4 answers)
Closed 3 years ago.
I am absolutely not into JavaScript and I have the following problem. I have a string variable (named url) containing an URL like this:
var url = https://smart.XXX.it/default.asp/YYY
Note that the YYY section of this URL can be different while the /default.asp/ section is always the same
I have to remove all this section from this URL: /default.asp/YYY
Basically I have to remove /default.asp/ and all what follows this section.
How can I implement this behavior?
var url = "https://smart.XXX.it/default.asp/YYY";
console.log("original URL " + url);
console.log("updated URL " + url.split("/default.asp")[0]);
Something like this?
const url = "https://smart.XXX.it/default.asp/YYY";
console.log(url.replace(/default\.asp.+/, ''));
This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 6 years ago.
i have a url like this
test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14
i want to remove the parameter using the following javascript
function removeParam(uri) {
uri = uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');
return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}
but it didn't work, anyone know what's wrong with that?
in modern browsers you can do this quite simply
var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri
at least, I think it's simpler
Your RegExp is no match!
If you want remove end_date, you should:
uri.replace(/(end_date=)([^*&]+)/, 'end_date=')
And so on.
This question already has answers here:
Remove querystring from URL
(11 answers)
Closed 7 years ago.
How would I remove part of a URL with jQuery, when the said part is changeable?
E.g. how can I remove this from my URL: ?modal=name
when 'name' might be name1 name2 or name3
I guess I will need to split the URL, but how do I do this when the modal name might be anything?
var url = 'http://yourdomain.com/search?modal=name';
alert(url.substring(0, url.indexOf('?')));
Demo
As you have asked "How to remove the part of a URL" using jQuery
Here is a basic workaround for you:
//the URL ( decode it just for GOOD practice)
var someRandomUrl = decodeURI("http://example.com?modal=name");
//here we do the splitting
var splittedParts = someRandomUrl.split("?");
// the first part of the array will be URL you will require
var theURL = splittedParts[0];
// the second part of the array will be the Query String
var theQuery = splittedParts[1];
alert(theURL);
This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found