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;
Related
I want export two pages of a same spreedsheet to one single file, how can i do it?
var ssID = "ssID"
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=xlsx&gid=AAAA";
var url2 = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=xlsx&gid=BBBB";
var params = {method:"GET", headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url, params);
DriveApp.createFile(response).setName(name);
I believe your goal is as follows.
You want to select 2 of all sheets in a Google Spreadsheet and export it as one XLSX file using Google Apps Script.
In this case, how about the following sample script?
Sample script:
function myFunction() {
var name = "sample.xlsx"; // Please set the output filename.
var ssID = "###"; // Please set your Spreadsheet ID.
var sheetIds = [12345, 67890]; // Please set the sheet IDs.
// 1. Create new Spreadsheet as a temporal.
var temp = SpreadsheetApp.create("temp");
var tempId = temp.getId();
// 2. Copy the selected sheets to the temp Spreadsheet.
SpreadsheetApp.openById(ssID).getSheets().filter(s => sheetIds.includes(s.getSheetId())).forEach(s => s.copyTo(temp).setName(s.getSheetName()));
temp.deleteSheet(temp.getSheets()[0]);
// 3. Export the temp Spreadsheet as a XLSX file.
var url = "https://docs.google.com/spreadsheets/d/" + tempId + "/export?format=xlsx";
var params = { method: "GET", headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() } };
var response = UrlFetchApp.fetch(url, params);
DriveApp.createFile(response).setName(name);
// 4. Remove the temp Spreadsheet.
DriveApp.getFileById(tempId).setTrashed(true);
}
References:
filter()
forEach()
copyTo(spreadsheet) of Class Sheet
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');
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.