How do I properly encode a mailto link with JSON data in the query parameters so that the link works as expected when some of the JSON data possibly includes spaces?
Here is a simple example:
var data = {
"Test": "Property with spaces"
};
var mailTo = 'mailto:?body=http://www.google.com/?body=' + JSON.stringify(data);
document.getElementById("link").href = mailTo;
The resulting link in the email after clicking the link looks like this:
Here is a JSBin showing what I am talking about:
https://jsbin.com/vuweyemeji/1/edit?html,js,output
Edit: Adding encodeURI() or encodeURIComponent() doesn't seem to work for me. I tried adding either of those methods around the data object and when I click the mailto link the url still looks the same in outlook.
You need to use encodeURIComponent twice, because you are encoding a parameter inside another parameter.
Your link is using the mailto protocol and using a body parameter which content should be encoded. But, inside that content you are entering a URL which has parameters, so, this parameters should be encoded also.
Try this:
var data = {"Test": "Property with spaces"};
var mailTo = 'mailto:?body=' + encodeURIComponent('http://www.google.com/?body=' + encodeURIComponent(JSON.stringify(data)));
document.getElementById("link").href = mailTo;
<a id='link'>anchor</a>
Related
This is a simple to understand question, I will explain step by step as to make everything clear.
I am using the Google Feed API to load an RSS file into my JavaScript application.
I have a setting to bypass the Google cache, if needed, and I do this by appending a random number at the end of the RSS file link that I send to the Google Feed API.
For example, let's say this is a link to an RSS:
http://example.com/feed.xml
To bypass the cache, I append a random number at the end as a parameter:
http://example.com/feed.xml?0.12345
The whole url to the Google Feed API would look like this, where "q" is the above link:
https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&q=http://example.com/feed.xml?0.12345
This bypasses the cache and works well in most cases but there is a problem when the RSS link that I want to use already has parameters. For example, like this:
http://example.com/feed?type=rss
Appending the number at the end like before would give an error and the RSS file would not be returned:
http://example.com/feed?type=rss?0.12345 // ERROR
I have tried using "&" to attach the random number, as so:
http://example.com/feed?type=rss&0.12345
This no longer gives an error and the RSS file is correctly returned. But if I use the above in the Google Feed API url, it no longer bypasses the cache:
https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&q=http://example.com/feed.xml&0.1234
This is because "0.1234" is considered a parameter of the whole url and not a parameter of the "q" url. Therefore "q" remains only as "http://example.com/feed.xml", it is not unique so the cached version is loaded.
Is there a way to make the number parameter be a part of the "q" url and not a part of the whole url?
You need to use encodeURIComponent like this:
var url = 'http://example.com/feed.xml&0.1234';
document.getElementById('results').innerHTML = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&q=' + encodeURIComponent(url);
<pre id="results"></pre>
You are escaping the special characters that would have been treated as part of the url otherwise.
To append or create a queryString:
var url = 'http://example.com/feed.xml';
var randomParameter = '0.1234';
var queryString = url.indexOf('?') > - 1;
if(queryString){
url = url + '&' + randomParameter;
} else {
url = url + '?' + randomParameter;
}
//url needs to be escaped with encodeURIComponent;
You need to use encodeURIComponent to do this.
encodeURIComponent('http://example.com/feed.xml&0.1234')
will result in
http%3A%2F%2Fexample.com%2Ffeed.xml%260.1234
and when appended to the end result you'll get
https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&q=http%3A%2F%2Fexample.com%2Ffeed.xml%260.1234
I am getting a json string parameter from my javascript files as follows :
http://local/action/?action=loadFile¶ms=%7B%22fileChart%22%3A%22url%22%2C%22item%22%3A%7B%22DocId%22%3A%2270078903%22%2C%22Headline%22%3A%22Alert%253A%2520AAPL%253A%2520QA%2520Test%22%2C%22PrimaryTickers%22%3A%22AAPL.O%22%2C%22ArrivalDate%22%3A%222015-04-29T08%3A04%3A40Z%22%2C%22fileType%22%3A%22url%22%2C%22secondaryFileType%22%3A%22pdf%22%2C%22secondaryFileExtension%22%3A%22pdf%22%2C%22curDate%22%3A1435589346483%2C%22Pages%22%3A8%2C%22url%22%3A%22https%253A%252F%252Fuat.citivelocity.com%252Frendition%252Feppublic%252FdocumentService%252FdXNlcl9pZD0mYWN0aW9uPXZpZXc%252FZmlsZV9uYW1lPVZTNkoucGRm%22%2C%22contributor%22%3A%22Citi%20-%20Linkback%20Test%22%7D%7D&requestType=GET×tamp=1435589346484
I want to display only the url part of the above url, such that I can decode https for the given link and display only the required link in a webview loadurl. In the case of the above link it's : https://uat.citvelocity.com ..etc ,
The current idea I got from searching online is something like this :
webview.loadurl("javascript:(function() { "| "document.getElementByTagName('url')[0].style.display="+";"+"})()"); }
});
But that doesn't work as expected. Am I doing it wrong? Any ideas ?
Since your string is JSON, it's probably a good idea to just parse that JSON into an object so you can just use the objects properties to get whatever you want.
var decoded = decodeURIComponent('http://local/action/?action=loadFile¶ms=%7B%22fileChart%22%3A%22url%22%2C%22item%22%3A%7B%22DocId%22%3A%2270078903%22%2C%22Headline%22%3A%22Alert%253A%2520AAPL%253A%2520QA%2520Test%22%2C%22PrimaryTickers%22%3A%22AAPL.O%22%2C%22ArrivalDate%22%3A%222015-04-29T08%3A04%3A40Z%22%2C%22fileType%22%3A%22url%22%2C%22secondaryFileType%22%3A%22pdf%22%2C%22secondaryFileExtension%22%3A%22pdf%22%2C%22curDate%22%3A1435589346483%2C%22Pages%22%3A8%2C%22url%22%3A%22https%253A%252F%252Fuat.citivelocity.com%252Frendition%252Feppublic%252FdocumentService%252FdXNlcl9pZD0mYWN0aW9uPXZpZXc%252FZmlsZV9uYW1lPVZTNkoucGRm%22%2C%22contributor%22%3A%22Citi%20-%20Linkback%20Test%22%7D%7D&requestType=GET×tamp=1435589346484'),
json = JSON.parse(decoded.slice(decoded.indexOf('¶ms=') + 8, decoded.indexOf('&requestType'))),
url = decodeURIComponent(json.item.url);
Im struggling with passing the parameter on the URL Address bar...
heres what I have:
http://win-t8o9hquvjcf/Reports/Pages/Report.aspx?ItemPath=%2fDatasheetforOMManual&ProjectReference=65656
ProjectReference is the parameter wihin the Report Builder.. what am i doing wrong here?
worked it out, you have to use the ReportServer URL link, not the Reports on its own...
http://yourservername/ReportServer/Pages/ReportViewer.aspx?%2fdatabasename%2freportname&rs:Command=Render¶meter=1
adding &rs:Format=PDF displays it as a PDF...
and for Lightswitch it works using the following:
var param1 = screen.tablename.param1
window.open("http://yourservername/ReportServer/Pages/ReportViewer.aspx?%2fdatabasename%2freportname&rs:Command=Render¶meter=1" + param1 + "&rs:Format=PDF");
I'm trying to encode my uri by using encodeURIComponent function. Here is my code.
//res[0].CLIENT_ID=10 and id=res[0].CLIENT_ID
var url = "new_quotation.php?clientid="+res[0].CLIENT_ID+""eid="+id;
var encodedurl = encodeURIComponent(url);
$('#edit').attr("href", encodedurl);
It successfully encodes the uri, but when the page redirects it shows error as
The requested URL /Quotation/new_quotation.php?clientid=10"eid=0000000014 was not found on this server.
I saw the url. It seems like
http://localhost/Quotation/new_quotation.php%3Fclientid%3D10%26quoteid%3D0000000014
So, the uri is encoded but why not the page is redirected? Do I need to use any other function to redirect? Or is there any error in my code?
You should only be encoding the values, not the entire url.
var url = "new_quotation.php?clientid="+encodeURIComponent(res[0].CLIENT_ID)+""eid="+encodeURIComponent(id);
$('#edit').attr("href", url);
Since you are using jQuery, you can also use param()
Each and every parameter in the URL must be applied encodeURIComponent (if the parameter consists of special characters)
Example :
var enc =param1+'/'+encodeURIComponent(param2)+'/'+encodeURIComponent(param3);
param2, param3 - here are expected to have special chars
I am hitting a struts action using AJAX, everything is fine but there is problem with Firefox , when i am passing the parameter in URL as a request parameter and if that parameter, contains hash(#) symbol in the end, then firefox strips everything after that symbol and send that parameter to action without it.
For example, if im passing test123#abcd in Firefox, then i am getting only test123 in action class as opposed to test123#abcd which is undesirable for my requirement.For IE it is working perfectly.Is there any way by which i can extract the full parameter including the # symbol in Firefox.
please let me know if i need to post the java action code also,thanks.
JS snippet
var valuePassword=test123#abcd;
var url = "/test/ChangePwdAjax.do?newPass="+valuePassword;
var xmlHTTP = getXMLHTTPRequest();
Use
var url = "/test/ChangePwdAjax.do?newPass="+ encodeURIComponent(valuePassword);
This will encode your valuePassword to a valid URL component which can be passed as a query string in URLs
And on the other side you should use decodeURIComponent to get the value from encoded string
var value = decodeURIComponent(valuePasswordPassed);
To know more about this Go here
When you change data you have to do a http POST request. Not a GET request. This will automatically solve your problem without having to encode your password.
xmlhttp.open("POST", "/test/ChangePwdAjax.do", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("newPass=" + valuePassword);
Surely worth mentioning that passing a password as a URL parameter is a terrible idea. A passer-by can see your new password in your browser's address bar. And anyone in the future can find the password in your browsing history.
-mobailey