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');
Related
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;
I can create an object URL like this:
let pixel = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let data = atob(pixel);
let buffer = new ArrayBuffer(data.length);
let array = new Uint8Array(buffer);
for (var i = 0;i<data.length;i++){
array[i]=data.charCodeAt(i);
}
let blob = new Blob([buffer], {type: 'image/jpeg'});
let url = URL.createObjectURL(blob);
And I can revoke an object URL like this:
URL.revokeObjectURL(url);
But what I would like to do is automatically revoke the URL after one use.
Is there a way to check if an object URL has been accessed?
Thank you in advance.
EDIT:
In this case, I can't just trigger a function when I use the URL. Instead, I would like to be able to check if it has been used even OUTSIDE of the page. An example would be if someone typed the URL into the address bar, this can't call javascript AFAIK.
If you don't have an API to do this, you could use localStorage to track this.
You could have 2 methods,
addUrlStatus - which takes a url as input and checks whether it has been accessed in the past.
updateUrlStatus - which sets the status of a url to true in localStorage once the user has used the url (not sure what action defines this in your code).
So you could do something like,
let pixel = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let data = atob(pixel);
let buffer = new ArrayBuffer(data.length);
let array = new Uint8Array(buffer);
for (var i = 0;i<data.length;i++){
array[i]=data.charCodeAt(i);
}
let blob = new Blob([buffer], {type: 'image/jpeg'});
let url = URL.createObjectURL(blob);
addUrlStatus(url);
function addUrlStatus(url) {
var _url = localStorage.getItem(url);
if(!_url) {
localStorage.setItem(url, false); // false indicates it hasn't been used yet.
return;
}
}
// this function needs to be called when the user has used the url.
function updateUrlStatus(url) {
localStorage.setItem(url, true);
URL.revokeObjectURL(url);
}
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
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();
How can I get the filename from a given URL in PhoneGap?
In JavaScript, I used something like this:
var uri = encodeURI("http://www.openerpspain.com/descargas-documentacion?download=2");
My application downloads the file, but I have to manually set the file name. To illustrate, when I call
onclick="descarga('http://www.openerpspain.com/descargas-documentacion?download=2')"
this function is run:
function descarga(URL){
var rutaarchivo = "file:///sdcard/data/com.protocolo/test1.pdf";
alert(rutaarchivo);
var filetransfer = new FileTransfer();
filetransfer.download(URL, rutaarchivo,
function(entry){
alert("Download complete : " + entry.fullPath);
},function(error) {
alert("download error source " + error.source);
});
}
This saves the download to ../com.protocolo, and its filename is test.pdf. I want to be able to save it as the name it is set as on the server (*manual_openerp.230209.pdf*) at the real URL, *...//www.openerpspain.com/descargas/manual_openerp.230209.pdf*.
How do I do that?
String fileName = url.substring(url.lastIndexOf('/') + 1);
where url is the complete path of your file.
Use:
url="file:///sdcard/data/com.protocolo/test1.pdf?getVar=value";
url.replace(/\?.*$/,"").replace(/.*\//,"");
This is basically asking How to get the file name from a full path using JavaScript?. Here, the solution was:
var filename = url.replace(/^.*[\\\/]/, '');
I added in my URLs parameters. Then in PhoneGap I used:
function getParameterByName( name,href ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Called from :
var filename = getParameterByName ("nombrefichero", url);
So this way I can get example.pdf from:
http://www.download.com/fake/example.pdf?nombrefichero=example.pdf
And then I can download it, and save it where I want.