0

EDIT 11/20/2017: The code provided by LOVEZ works great but is there a way to 'refresh' the screen to avoid multiple "Get Link" on screen? enter image description here

As a workaround until I can figure out how to refresh the page, I just added line 10 and changed line 12

enter image description here

---ORIGINAL POST BELOW---

I'm trying to create a dynamic mailto link based on user input when I hover over the "Get Link" it seems to generate it correctly, but the link in the email body is cut off. Below code is what I tried but it seems to refresh my SharePoint page after it generates the link.

<input id="myInput" type="text">
<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {

var myID = document.getElementById("myInput").value;
    var msgBody = "https://tenant.sharepoint.com/Lists/ListName/Item/editifs.aspx?DefaultView=ManagerComments&ID="+myID+"&SOURCE=https://tenant.sharepoint.com/SitePages/SitePage.aspx";
var myLink = "mailto:?subject=Pending Approvals&body="+msgBody;



var x = document.createElement("A");
var t = document.createTextNode("Get Link");
x.setAttribute("href", myLink);
x.appendChild(t);
document.body.appendChild(x);
}
</script>

enter image description hereenter image description here

1

Use the code below:

<input id="myInput" type="text">
<button onclick="myFunction()" type="button">Try it</button>
<span id="GetLink"></span>
<script type="text/javascript">
function myFunction() {
    var myID = document.getElementById("myInput").value;
    var msgBody = "https://tenant.sharepoint.com/Lists/ListName/Item/editifs.aspx?DefaultView=ManagerComments&ID="+myID+"&SOURCE=https://tenant.sharepoint.com/SitePages/SitePage.aspx";
    var myLink = "mailto:?subject=Pending Approvals&body="+encodeURIComponent(msgBody);
    var linkHtml="<a href='"+myLink+"'>Get Link</a>";
    document.getElementById("GetLink").innerHTML=linkHtml;
}
</script>

enter image description here

2
  • It works but is there a way to refresh the screen (please see original post for added question). Thanks LOVEZ!! Nov 20 '17 at 22:37
  • I modify the code above, it will solve this issue.
    – LZ_MSFT
    Nov 23 '17 at 0:57
0

Try using the javascript encodeURIComponent() method:

var msgBody = encodeURIComponent("https://tenant.sharepoint.com/Lists/ListName/Item/editifs.aspx?DefaultView=ManagerComments&ID="+myID+"&SOURCE=https://tenant.sharepoint.com/SitePages/SitePage.aspx");

You may also need to double-encode the value of SOURCE= as well, depending on how that parameter is being used.

0

To avoid multiple Get Links on button click, use below:

Add $("#GetLink a").remove();

before this:

var myID = document.getElementById("myInput").value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.