Is that possible to give mailto inside the body of another mailto?
I have vb.net code through which I am opening outlook window.
I have the below code,
sMsg = User.Redirect("mailto:" + legRev + "& CC=" + cc + "&Subject= " + OuterSubject + "&body=" + Body)
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "showalert", sMsg, True)
Public Function Redirect(ByVal PageName As String) As String
Dim sb As New StringBuilder()
sb.Append("window.location.href='" + PageName + "'; ")
Return sb.ToString()
End Function
In the body string I have
mailto:" + innerTo + "&CC=" + innerCC + "&Subject= " + innerSubject
Problem is I am getting a mail opened with subject set to 'innerSubject' instead of 'OuterSubject'
I think my OuterSubject is getting replaced by InnerSubject.
The body string needs to be escaped with a function like Uri.EscapeDataString. Your URI should end up having only one ? in it, and none of the & characters from your body should be visible.
Example:
mailto:john.doe#example.com?subject=Test+Message&body=mailto%3Ajane.doe%40example.com%3Fcc%3Dbob%40bob.com%26subject%3DReply%2Bto%2Byou
Related
I want to prepare an email to send with mailto:
This email contains a few words and a js script. This script does not need to be executed. It's just for the receiver to copy and paste.
The script :
<script id="myID">var script = document.createElement("script");script.src="script-to-inject.js?id=myID&type=0&name=Name&size=120";document.head.appendChild(script); </script>
And my mailto:
window.location.href = "mailto:"+email+"?subject="+subject+"&body=FewWords"+ script;
When my mail isopen i have something like that :
<script id="myID">var script = document.createElement("script");script.src="script-to-inject.js?id=myID
The end of the script does not appear (after the first &)
How can i fix this ?
Thanks !
You forgot to encode the URL parameters, so the & starts the next parameter.
You can use the encodeURIComponent function:
window.location.href = "mailto:" + encodeURIComponent(email) +
"?subject=" + encodeURIComponent(subject) +
"&body=" + encodeURIComponent("FewWords" + script);
Another, cleaner, way would be to use URLSearchParams:
const url = new URL(`mailto:${encodeURIComponent(email)}`)
url.searchParams.set('subject', subject)
url.searchParams.set('body', 'FewWords' + script)
window.location.href = url
You need to be escaping email, subject, and script properly when setting the href attribute. What if these variables contain the & or the = characters? You can see how this would get misinterpreted.
Try this:
window.location.href = "mailto:"
+ encodeURIComponent(email)
+ "?subject="
+ encodeURIComponent(subject)
+ "&body=FewWords"
+ encodeURIComponent(script);
(I'm not sure that you can pass HTML in the body parameter, by the way, it might get interpreted as plain text.)
You can also use URLSearchParams:
const params = new URLSearchParams();
params.append('subject', subject);
params.append('body', 'FewWords' + script);
window.location.href = 'mailto:' + encodeURIComponent(email) + '?' + params.toString();
Hi In my webpage I generate the information and using mailto for opening the mail on outlook for the user modify the email. It worked fine. However when the body or subject has apostrophe that cause problem, so I used Server.UrlEncode to encode the string. Now, the space show '+' and the new line show '\n'. If I don't use Server.UrlEncode, the function is not called.
There is my code to call the javascript function in vb.net
Dim strSubject As String = Server.UrlEncode(strName)
Dim strBody As String = Server.UrlEncode("it's your order list:" & "\r\n" & strList)
Dim script As String = "MailtoOrder(''," & "'" & strSubject & "', '" & strBody & "')"
If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "mail") Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "mail", script, True)
End If
There is my javascript:
function MailtoOrder( to, subject, body) {
var email='';
if (to != undefined) {
email=to;
}
email = email + '&subject=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(body);
window.location.href = "mailto:" + email;
}
I get away using Server.UrlEncode. I just replace the apostrophe to be escaped character in strSubject and strBody string. it works.
Heyho, I want to add GET params like?USER_NAME=Max&USER_ID=01 to the URL if the page gets reloaded. I already got a few snippets for changing the URL / adding params. But I need something to detect the page reload and execute a function. Or a way to change the path of the Url directly if the page gets reloaded.
I tried several things like beforeunload and navigation types.
$(window).bind('beforeunload', function() {
// EDIT: vars like uname etc are defined
var newurlgetstring = "?USER_NAME=" + uname + "&USER_EMAIL=" + uemail + "&LAST_NAME=" + lname + "&PRE_NAME=" + fname + "&UTITEL=" + utitel + "&U_ID_T=" + uidt + "&MV=" + mv + "&V=" + uv ;
var newurl = window.location.protocol + "//" + window.location.host +window.location.pathname + newurlgetstring;
window.history.pushState({path:newurl},'',newurl);
});
But I want able to execute a function with beforeunload. I was just able to display a return question.
I'm using JSPs to create dynamic web pages...
At the beginning of one of my forms, I have some javascript that needs to run to initialize the page with given attributes.
I'm creating a Java String in the JSP <% %> blocks that I want to pass to the initializePage javascript function.
Here's the code:
<script>
$(document).ready(function(){
<%String algorithmXMLPath = request.getContextPath() + "/" + PePw.PATH_ALGORITHM_XMLS;
String initParms = "'" + algorithmXMLPath + "'," +
" '" + Utilities.getString(reqBean.getMachineType()) + "'," +
" '" + Utilities.getString(reqBean.getModel()) + "'," +
" '" + Utilities.getString(reqBean.getReasonCode()) + "'";%>
initializePage(<%=initParms%>);
});
</script>
This results in a source code of:
initializePage('/PePasswords/data/algorithmXMLs/', '', '', '');
When I run this, I get an error in the FF error console "Unterminated String literal" and it points to the end of the initializePage call... When I click the link in the error console, it actually points to the line with });
Not sure what i'm doing wrong here...
Looks like one of the variables had a hidden new line "\n" being passed into the JSP call...
I replaced
Utilities.getString(reqBean.getReasonCode())
with
Utilities.getString(reqBean.getReasonCode()).replace("\n", "").trim()
I have some other javascript functions that are being set on the onfocus and onblur events of the textbox that I am using. In these functions it calls a generic javascript function that is not related to any controls. I want to know how to just simply spit this function out to the html of the page from the code behind. Something like this...
Page.ClientScript.RegisterStartupScript(this.GetType(), "?????", getCounter);
EDIT: Here is what I mean
public class MVADTextBox : TextBox
{
protected override void OnLoad(EventArgs e)
{
var getCounter = "<script language=\"javascript\">" +
"function GetCounter(input) {" +
//this function gets the number of special characters taht are in a row.
//it is only the grouping of characters that are right after your current position
"var textbox = document.getElementById(input.id);" +
"var mask = textbox.getAttribute('Mask');" +
"var inputCharacters = textbox.getAttribute('InputCharacters');" +
"var tbid = \"#\" + input.id;" +
"var position = $(tbid).caret().start;" +
"var counter = 0;" +
"for (var i = position; i < mask.length; i++) {" +
" if (mask[i] != '#') {" +
" counter++;" +
" if (mask[i + 1] == '#') {" +
" break;" +
" }" +
" }" +
"}" +
"return counter;" +
" }" +
"</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnFocus", onFocus);
Page.ClientScript.RegisterStartupScript(this.GetType(), "GetCounter(input)", getCounter);
var onBlur = "<script language=\"javascript\"> function PopulateField(input) {if (input.value == \"\") {input.value = input.defaultValue; input.className = 'sampleText'; } } </script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnFocus", onFocus);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnBlur", onBlur);
}
}
The on blur method is getting sent to the page.
Answer:
I believe that Page.ClientScript has been deprecated. You should be using ClientScriptManager.
Replace your "?????" with the name of the script. Honestly, the name of the script is almost useless (unless you need to check for its existence later on).
ClientScriptManager.RegisterStartupScript(this.GetType(), "myCount", getCounter);
Usage Clarification:
//You must surround your code with script tags when not passing the bool param
ClientScriptManager.RegisterStartupScript(this.GetType(),
"myCount",
"<script>alert('Hey')</script>");
// The last param tells .Net to surround your
// code with script tags (true) or not (false)
ClientScriptManager.RegisterStartupScript(this.GetType(),
"myCount",
"alert('Hey')", true);
Additional Information:
Signatures from MSDN:
public void RegisterStartupScript(
Type type,
string key,
string script
)
public void RegisterStartupScript(
Type type,
string key,
string script,
bool addScriptTags
)
See: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx
I think you need to use the ClientScriptManager.RegisterClientScriptBlock method
Try this
EDITED:
var getCounter = "<script language=\"javascript\">" +
"function GetCounter(input) {" +
//this function gets the number of special characters taht are in a row.
//it is only the grouping of characters that are right after your current position
"var textbox = document.getElementById(input.id);" +
"var mask = textbox.getAttribute('Mask');" +
"var inputCharacters = textbox.getAttribute('InputCharacters');" +
"var tbid = \"#\" + input.id;" +
"var position = $(tbid).caret().start;" +
"var counter = 0;" +
"for (var i = position; i < mask.length; i++) {" +
" if (mask[i] != '#') {" +
" counter++;" +
" if (mask[i + 1] == '#') {" +
" break;" +
" }" +
" }" +
"}" +
"return counter;" +
" }" +
"</script>";
this.TextBox1.Attributes.Add("OnFocus", "GetCounter(this);");
if (!ClientScript.IsClientScriptBlockRegistered("getCounter")) {
ClientScript.RegisterClientScriptBlock(this.GetType(), "getCounter", getCounter, false);
}
You would put the actual function definition, which you already have in getCounter. Note that the second parameter which you currently have as "????", as James pointed out, is for the script's key, which must be unique from all other scripts registered for this type. The third parameter is the script itself, and the fourth determines whether script tags are to be added, which needs to be false, since you already added them.
Page.ClientScript.RegisterStartupScript(this.GetType(),
"someKeyForThisType", getCounter, false);