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.
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();
I need exactly like I asked in question title.
I have a javascript file for calculating Thanksgiving date for every year, provided the year value input.
Now I want this javascript to be executable by Windows Script Host and also allowed to create as many InputBoxes as possible without changing the file type or extension.
I tried below code but it gives me error expected ;:
ws = WScript.CreateObject('WScript.Shell');
var myDate = new Date();
myDate.setHours(0, 0, 0, 0);
// Function WSHInputBox(Message, Title, Value)
// ' Provides an InputBox function for JScript
// ' Can be called from JScript as:
// ' var result = WSHInputBox("Enter a name", "Input", test);
// WSHInputBox = InputBox(Message, Title, Value)
// End Function
strYear = GetUserInput( "Enter some input:" )
ws.Popup('Year ?'+strYear);
//var strYear = WSHInputBox("Enter the year", "Thanksgiving Year")
myDate.setYear(parseInt(strYear));
// Determine November 1.
myDate.setDate(1);
myDate.setMonth(10);
// Find Thursday.
var thursday = 4;
while(myDate.getDay() != thursday) {
myDate.setDate(myDate.getDate() + 1);
}
// Add 3 weeks.
myDate.setDate(myDate.getDate() + 21);
ws.Popup('Result: ' + myDate);
Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Check if the script runs in CSCRIPT.EXE
If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
' If so, use StdIn and StdOut
WScript.StdOut.Write myPrompt & " "
UserInput = WScript.StdIn.ReadLine
Else
' If not, use InputBox( )
UserInput = InputBox( myPrompt )
End If
End Function
Function GetUserInput( myPrompt )
' This function uses Internet Explorer to
' create a dialog and prompt for user input.
'
' Version: 2.11
' Last modified: 2013-11-07
'
' Argument: [string] prompt text, e.g. "Please enter your name:"
' Returns: [string] the user input typed in the dialog screen
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Error handling code written by Denis St-Pierre
Dim objIE
' Create an IE object
Set objIE = CreateObject( "InternetExplorer.Application" )
' Specify some of the IE window's settings
objIE.Navigate "about:blank"
objIE.Document.title = "Input required " & String( 100, "." )
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 320
objIE.Height = 180
' Center the dialog window on the screen
With objIE.Document.parentWindow.screen
objIE.Left = (.availWidth - objIE.Width ) \ 2
objIE.Top = (.availHeight - objIE.Height) \ 2
End With
' Wait till IE is ready
Do While objIE.Busy
WScript.Sleep 200
Loop
' Insert the HTML code to prompt for user input
objIE.Document.body.innerHTML = "<div align=""center""><p>" & myPrompt _
& "</p>" & vbCrLf _
& "<p><input type=""text"" size=""20"" " _
& "id=""UserInput""></p>" & vbCrLf _
& "<p><input type=""hidden"" id=""OK"" " _
& "name=""OK"" value=""0"">" _
& "<input type=""submit"" value="" OK "" " _
& "OnClick=""VBScript:OK.value=1""></p></div>"
' Hide the scrollbars
objIE.Document.body.style.overflow = "auto"
' Make the window visible
objIE.Visible = True
' Set focus on input field
objIE.Document.all.UserInput.focus
' Wait till the OK button has been clicked
On Error Resume Next
Do While objIE.Document.all.OK.value = 0
WScript.Sleep 200
' Error handling code by Denis St-Pierre
If Err Then ' user clicked red X (or alt-F4) to close IE window
IELogin = Array( "", "" )
objIE.Quit
Set objIE = Nothing
Exit Function
End if
Loop
On Error Goto 0
' Read the user input from the dialog window
GetUserInput = objIE.Document.all.UserInput.value
' Close and release the object
objIE.Quit
Set objIE = Nothing
End Function
WScript.Quit();
Can someone help out as I am stuck at how to include both types of code in this same Javascript file, I know there is some way, but what's that ?
Problem
Developer VickyDev wants to run a MSFT VBScript function within a MSFT JScript file
InputBox is a VBScript function and not a JScript function
// Function WSHInputBox(Message, Title, Value)
// ' Provides an InputBox function for JScript
// ' Can be called from JScript as:
// ' var result = WSHInputBox("Enter a name", "Input", test);
// WSHInputBox = InputBox(Message, Title, Value)
// End Function
Solution
Combine VBScript and JScript together using windows script host file with language declaration on script tags.
Make sure to declare all the VBScript functions first before declaring the JScript
Example
<?xml version="1.0"?>
<package>
<job id="uu244trumpehant">
<script language="vbscript">
<![CDATA[
Function HelloWorldVB
msgbox "hello world from VB"
End Function
]]>
</script>
<script language="jscript">
<![CDATA[
HelloWorldJS();
HelloWorldVB();
//HelloWorldVBTwo(); // this one will error
// function defined below
function HelloWorldJS(){
WScript.Echo("hello world from JS");
}
]]>
</script>
<script language="vbscript">
<![CDATA[
Function HelloWorldVBTwo
msgbox "hello world from VBTwo"
End Function
]]>
</script>
</job>
</package>
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
i need help with adding space or a new row in a script. It looks like this:
function myFunction() {
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var sender = message.getFrom();
var body = "This email is sent from:" + " " + sender + message.getPlainBody();
var subject = message.getSubject();
var attachment = message.getAttachments();
GmailApp.sendEmail("user.name#aruba.com", subject, body, {attachments: attachment});
}
I need to add the "message.getPlainBody();" on a new line under the "This email is sent from:" + " " + sender "
Right now all gets on the same row in the Email. Is there any similar thing like the html tag <br> ?
You can insert linebreak/newline characters in strings to start a new line.
Note the \n which actually counts as 1 character (the \ starts an escape sequence)
var body = "This email is sent from:\n" + sender + "\n\n" + message.getPlainBody();
Just try with adding \n:
var body = "This email is sent from: " + sender + "\n" + message.getPlainBody();
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()