how to get parameter value from a url using js [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query string values in JavaScript
the link is
http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27
how can I parse the parameter "Text"(at the end) from the url using javascript?
anyone can help me?

URI.js is a small library that makes working with URLs nice and easy :)
var url = new URI("http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27");
var queryMap = URI.parseQuery(url.query());
var text = queryMap["Text"];
console.log("Text: ", text);

You can combine window.location.search : https://developer.mozilla.org/en-US/docs/DOM/window.location
with indexOf : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
and substring: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

Related

Parse value only from JSON string [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);

Fix a regex that extract twitter id from a twitter url [duplicate]

This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
I have a bug in my wysiwyg editor.
I have a box where I past a tweet url, and I must extract the ID to replace it with the embed code.
regex: {
twitter: /https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
}
if (str.match(this.opts.regex.twitter)) {
parsed = str.replace(this.opts.regex.twitter, '<blockquote class="twitter-tweet tw-align-center">TWEET $1</blockquote>');
return parsed;
}
The bug is in my regex
/https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
If I take this url
https://twitter.com/howard3141/status/1330546000969273347
It doesn't work because 'howard3141' has some numbers inside.
Since you need to fetch from a URL, window.location provides great apis to get value.
Following is the demonstration of it.
Note: I'm using URL to create a location like object, but it should work with window.location as well
var url = new URL('', 'https://twitter.com/howard3141/status/1330546000969273347')
console.log(url.pathname.match(/\/([^\/]*)\//)[1])

Change value of query parameter using jQuery [duplicate]

This question already has answers here:
Updating existing URL querystring values with jQuery
(12 answers)
Closed 4 years ago.
I need to change the value of query parameter present in the given URL using jQuery.
There are 2 possible URL's like,
www.domain.com/?id=10&name=xxx
and
www.domain.com/?name=xxx&id=10
I need to change the value of parameter id in this URL into something like www.domain.com/?id=15&name=xxx. Regex seems to be the solution for this, but it looks confusing to me.
Possible solution:
Select the string between "id=" and "&" (or) string from "id=" to the end of string and replace it with desired value 15.
Does anyone have solution for this or any other better solution?
Thanks in advance!
var url="www.domain.com/?id=10&name=xxx";
changeUrl("id",15);
changeUrl("name","sumesh");
function changeUrl(key,value){
var patt = new RegExp(key+"=[a-zA-Z0-9]+");
var matches = patt.exec(url);
var id2 = matches[0];
url = url.replace(id2, key+'='+value);
console.log(url);
}
Use RegExp to achieve this.
Why don't you use simple replace?
var newUrl = location.href.replace("id="+10, "id="+15);
I found this solution from an answer to the question
Change url query string value using jQuery (marked as duplicate).

How to get url parameter and add active class witt javascript/jquery? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
My links are like that:
index.php
index.php?do=that
index.php?do=this
And my current javascript code is like that:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
console.log(url);
$('.nav-link').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
So with this code even if I'm at index.php?do=this console log says to me /path/index.php
I think I need make changes on var url = window.location.pathname line but I couldn't.
How should I improve my code? There are bunch of similiar questions here in SO, but not the same one.
I appreciate any help.
In PHP you can get your URL parameters from GET array.
$_GET['do'] or try to print $_GET. This array will print all the URL parameters.
Hope this helps!

Need a Javascript URL Regex [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Wondering if someone could help with out with creating a regex..
Basically, taking an iFrame's src, and seeing if it's from SoundCloud. If it is, return its id. For example:
var src = 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F85110490&auto_play=false&show_playcount=false&show_user=false&show_comments=false&buying=false&liking=false&sharing=false&show_artwork=false&color=00e7ff';
function soundcloudId(src) {
var p = /___________/;
return (src.match(p)) ? RegExp.$1 : false;
}
soundcloudId(src);
And as a result, it would run the "src" through the regex, and if a soundcloud link, would return 85110490. Otherwise, false.
Try this regex:
/http:\/\/w.soundcloud\.com\/.*%2Ftracks%2F([0-9A-F]+)/
Runnable example: http://jsfiddle.net/mYf6P/

Categories