How To store parameter of a url into variable in JavaScript - javascript

Hii I am Shashwat I want to know how to store parameter of a url into variable in JavaScript
My url is -- www.example.com?name=shashwat&lastname=mishra&email=example#gmail.com
So I want to store like this
var name = shashwat
var lastname = mishra
var email = example#gmail.com

You can use javascript code to get query parameter
function getUrlParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
var email = getUrlParameterByName('email');

<html>
<body>
</body>
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const product = urlParams.get('email')
console.log(product);
</script>
</html>
Please use this code to get url variable

Related

Need to change the parameter value in the url

trying to add/change the values of the parameters in the url, but can't able to change/add the values
trying to add/change the values of the parameters as shown below
var Filename ='abc.pdf';
var strUser='john';
var url = '#Url.Action("Action","Controller", new{ filename="name", username="User" })';
window.location.href = url.replace('name',Filename + 'User', strUser);
but not able to do it
try this
var url = new URL('http://demourl.com/path?id=100&topic=main');
var search_params = url.searchParams;
// new value of "id" is set to "101"
search_params.set('id', '101');
// change the search property of the main url
url.search = search_params.toString();
// the new url string
var new_url = url.toString();
// output : http://demourl.com/path?id=101&topic=main
console.log(new_url);
Try This:
var downloadUrl = '#Url.Action("Download", "File", new { filename = "default.pdf", username = "default" })';
var Filename = 'abc.pdf';
var strUser = 'john';
var url = downloadUrl.replace('default.pdf', Filename).replace('default', strUser);
window.location.href = url;

assign value to variable in getQueryStringParams

Below code gives me what I want but it gives undefined when stored in var- I would like the value assigned to a variable in getQueryStringParams. How to achieve this?
const url = location.href;
function getQueryStringParams(params, url) {
// first decode URL to get readable data
const href = decodeURIComponent(url || window.location.href);
// regular expression to get value
const regEx = new RegExp('[?&]' + params + '=([^&#]*)', 'i');
const value = regEx.exec(href);
// return the value if exist
return value ? value[1] : null;
};
getQueryStringParams('region', url);
You can achieve this easily using the URLSearchParams object like this
function getQueryStringParams(params) {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.get(params);
};
getQueryStringParams('region');

How can I pass a value in a URL and insert value in a new URL to redirect with Javascript?

I am passing a value in a URL in the form of http://example.com/page?id=012345. The passed value then needs to be inserted in a new URL and redirect the page to the new URL. Here is what I have been working with
function Send() {
var efin = document.getElementById("id").value;
var url = "https://sub" + encodeURIComponent(efin) + ".example.com" ;
window.location.href = url;
};
Sounds like you're looking for the features of URLSearchParams - Specifically using .get() to fetch specific parameters from the URL
// Replacing the use of 'window.location.href', for this demo
let windowLocationHref = 'http://example.com/page?id=012345';
function Send() {
let url = new URL(windowLocationHref);
let param = url.searchParams.get('id');
let newUrl = "https://sub" + encodeURIComponent(param) + ".example.com" ;
console.log('Navigate to: ' + newUrl);
//window.location.href = newUrl;
};
Send();

Get a parameter from a URL and pass it to a href in WordPress

I need to get 2 parameters in a URL. The URL will http://thisisurl.com?name=john&url=https://www.myrurl.com/
I need url=https://www.myrurl.com/ to be passed on to my href links on the page.
How can i do that?
Thanks
You can create method getParameterByName to get parameter and use .attr("href", url); to update href of a tag
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
$(document).ready(function(){
var myurl = 'http://thisisurl.com?name=john&url=https://www.myrurl.com/'
var url = getParameterByName('url', myurl);
console.log(url);
$('.test').attr("href", url);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class='test'>Test</a>

Java net url refused connection

I'm working with ARIS tool and I want to make calls(GET, POST...) in ARIS to ARIS API repository!
I have authentication that works when I try it directly in the repository, but I got an error when I debug the code I have in ARIS.
The error: Error running script: Connection refused: connect.
I have the following code:
var obj = new java.net.URL(url);
var con = obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", java.net.USER_AGENT);
var tenant = "";
var name = "";
var password = "";
var key = "";
var authString = tenant + ":" + name + ":" + password + ":" + key;
var encoder = new java.lang.String(Base64.encode(authString));
con.setRequestProperty("Authorization", "Basic" + encoder);
var responseCode = con.getResponseCode();
var iN = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
var inputLine = new java.lang.String();
var response = new java.lang.StringBuffer();
while((inputLine = iN.readLine()) != null){
response.append(inputLine);
}
iN.close();
return new java.lang.String(response);
Is the problem that I use Basic authentication, but I have tenant and key also or it's something else?
Also, name, password, key and tenant I'm leaving empty for security purposes, but in the original code the values are inserted. Also the url parameter contains the url link that is called directly in the repository.
Can someone please help me?
Thanks!

Categories