How to add an variable value in between the hyperlink in javascript - javascript

I am sending an email via javascript. In mail.parameter.text property I need to send the hyperlink. In the code below I am hardcoding the url whick looks very lengthy and also I need to add the /dashboard at the end of the url. Any idea on how to shorten the url?
var parent = space.getParent();
var siteGroup = "GROUP_EMAIL_CONTRIBUTORS";
var mail = actions.create("mail");
mail.parameters.to_many = siteGroup;
mail.parameters.subject="New Site Created in Community"
mail.parameters.text=" A new site called " + document.properties.name +"is created.Click http://pc1:8080/share/page/site/"+document.properties.name+"/dashboard"+" to join the site";
//execute action against a document
mail.execute(document);
Sorry if this is a basic question I dont know javascript. This is my entire script and I am not using any html tags in between.

You can change it to something like this, for example:
mail.parameters.text="Click <a href='http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard'>here</a>";
Your link will appear as "here" and nevertheless lead to the URL you specified. Is that what you mean by "shorten the url"?

Please try now.
var url = "Click http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard" + " to join the site";
ail.parameters.text = url;

Related

Get current URL and add something in front with JavaScript

For a multi-language website, I want two buttons for the two languages that exist on the website.
The standard url would be: mydomain.com/something (this would be in german for example) The english url for this is: mydomain.com/en/something
How can I set up the buttons to get the current url/ page (in this case /something and add /en in front of it? Everything I found was to add stuff after the full domain.
Thank you very much.
check Location Obj MDM docs
console.log(document.location.origin + "/en" + document.location.pathname)
I'm not sure what your end goal is but if it involves reloading the same page with the localisation in the URL (i.e. on button click the page reloads and the page's URL is changed to mydomain.com/en/something meaning that on your next button click, the page would need to reload and its URL would need to be mydomain.com/something again) then you may need to look into RegEx and running it against the current URL to then swap out the URLs in the button so that on click you go to the correct version of the domain.
If the end goal is to only get the current URL and toggle whether the localisation appears in the URL or not then take a look at the snippet below which should hopefully help you out a bit.
For some further reading, I think these resources may be helpful for you:
RegEx: Regular expressions guide
RegEx: Regex101 - this is essentially a playground for practicing and testing your RegEx.
Location Object: MDN or W3Schools
jQuery(document).ready(function($) {
var $languageToggle = $('#language-toggle');
var origin = location.origin;
var pathname = location.pathname;
$languageToggle.on('click', function() {
var $this = $(this);
var toLanguage = $this.data('to-language');
var currentLanguage = $this.data('current-language');
var localisation = '';
/**
* This if statement will help prevent the constructedUrl variable
* from ever having a url such as https://website.com//page
* (notice the double forward slashes after .com).
*/
if (toLanguage) {
localisation = '/' + toLanguage;
}
var constructedUrl = origin + localisation + pathname;
$this.data('to-language', currentLanguage);
$this.data('current-language', toLanguage);
$('#output').text(constructedUrl);
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output">Click on the toggle button</div>
<button id="language-toggle" data-to-language="en" data-current-language="">
Toggle language
</button>
var myFunc = () => {
window.location.href = window.location.href + "place you text here"
}
Click the button and the page will be edited
<button onclick="myFunc()">click me</button>

Google Apps Script, function to return text as hyperlink

I have a Google Apps Script function SearchFiles() which returns the download urls depending on the keyword the user provides. It is something like this:
function SearchFiles(inputValueFromUser) {
var searchFor ='title contains "' + inputValueFromUser + '"';
var searchForAbsString = inputValueFromUser + '.pdf' ;
//The Rest of the Code goes here
if(file.getName() == searchForAbsString){
downloadURL = "https://drive.google.com/uc?export=download&id=" + fileId;
arryOfUrls.push(downloadURL);
};
};
Logger.log('arryOfUrls: ' + arryOfUrls);
return arryOfUrls;//.toString(); //Send array of download urls back to client side JavaScript
};
The function is returning arryOfUrls in plain text.
1) How can it be sent in hyperlink form?
2) Is it possible to open the URL automatically(start download immediately) as one clicks the respective button( on Index.html) to get the link?
No it can't be sent as a hyperlink directly (at least not as far I know). But you can have the HTML information embedded into the string (like <a href=downloadUrl>...</a>"). This can be interpreted on the client side as a link.

exchange variables between html files in javascript

I am trying to share variables between two html pages. I am only using javascript and HTML5 to develop a windows 8 app. Based on an image which a user clicks on one page, I want a div on a second page to be populated with that image. Any ideas?
When I click on the image, I am currently calling the following function:
function imageClick(url) {
//var id = parsed.ClientID;
//window.location= url + "?" + id
window.location = url;
}
Then in my html file, I have this line:
<img onclick="imageClick('pages/page2/page2.html')"
data-win-bind="src:image" style="width: 133px; height: 125.5px;">
I was thinking of getting that id in the next page's url (if I were to uncomment the commented lines above) but it's a bit hackky and I don't actually know how to go about executing the retrieval of that on the next page..
Is there a more efficient and easy way of doing this in javascript? Like an equivalent of sessions in php or something?
Javascript does not have session variables because it runs on the client side. You can use URL parameters and cookies in order to achieve the same results.
You can get the URL parameter by using this function:
http://ziemecki.net/content/javascript-parsing-url-parameters
Add the link to the image to the query part of the url when they click. Something like you had in the comment, assuming you don't have a query part already:
function imageClick(url) {
//var id = parsed.ClientID;
window.location= url + "?src=" + url.src;
//window.location = url;
}
The other page can use window.location.search to extract it, strip off the src=. The code would look something like this:
var src = window.location.search;
if (src.indexOf("src=") == 0) {
src = src.substring(4);
}
newPageImageElement.src = src;
Where newPageImageElement is the <img> where you want to display the picture on the second page.

Javascript - reload page with different QueryString

I want to reload a page using JavaScript passing different parameters in URL.
My Page name is test1.aspx, I used:
window.location="test1.aspx?user=abc&place=xyz";
It's not working...!!!
window.location is an object. You need to access the href property on it, like this:
window.location.href="test1.aspx?user=abc&place=xyz";
If you need to send dynamic value, then
var user = "abc";
var place = "xyz";
window.location.href = "test1.aspx?user=" + user + "&place=" + place;
window.location = "path page?user=" + $("#txtuser").val();

Make a link non-secure

I have a JavaScript link that is as follows:
<a href="#" onclick="window.open('TrackPackage.asp?track=235&ship=OTHER&ShippingMethod=1', '', 'location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460'); return false;">
<span class="PageText_L288n">TRACK YOUR PACKAGE</span>
</a>
However, it is being linked from a secure page, so it becomes a secure page. Unfortunately this creates a problem for me since a unsecured form is automatically submitted when they arrive at the page. They get prompted: this form is being submitted insecurely. There isn't any sensitive data on those pages so I really don't need the page to be secured in the first place. Is there an attribute, either in HTML or JavaScript that could make a link not secure. I cannot really modify the link itself because it's a dynamic link.
However, I do believe some jQuery can add http://www.example.com/ before the TrackPackage.asp. I guess that would be acceptable, but I'd prefer some kind of attribute that will just make the link non secure. Thanks
You might try to override native function. But in some browsers that may not work (did not find such browser, so assume that it works in all, at least major, browsers :) ):
var windowOpen = window.open;
window.open = function(url, name, settings) {
var url = 'http://www.example.com/' + url;
alert("New url = " + url);
windowOpen(url, name, settings);
}
If you want to do it for a selected class of the links then you can do it like this (the idea of the onclick attribute modification as a text string is not the best one)
<script>
var windowOpen = window.open, clicked = false;
window.open = function(url, name, settings) {
if (clicked) var url = 'http://www.example.com/' + url;
alert("New url = " + url);
windowOpen(url, name, settings);
}
$(document).ready(function(){
$('span.packagetracklink').click(function(){
clicked = true;
setTimeout(function(){clicked = false;}, 200);
});
});
</script>
<a href="#" onclick="window.open('some_url', '', ''); return false;">
<span class="packagetracklink">TRACK YOUR PACKAGE</span>
</a><br>
<a href="#" onclick="window.open('some_url', '', ''); return false;">
<span class="another_class">TRACK YOUR PACKAGE</span>
</a>
If you receive dynamically the .asp page before using it in the window.open you could just make a String replace to substitute the "https" for an "http" protocol in the URL or in case there's any protocol, add the "http" at the beginning of if.
I haven't tried this since I don't have an https page to play with but I think it can work.
You need to somehow be able to get the instance of that A tag first. It doesn't have an ID or CLASS, so you're on your own there (unless you paste parent element code and siblings code, at least snippets.
var link = $("a"); // I don't have enough info to tell you how to precisely get this instance
var originalOnClick = link.attr("onclick");
var part1 = "window.open('"; // this is always the same, right?
var part2 = originalOnClick.substr(part1.length); // the remainder, beginning with TrackPackage.asp
var newOnClick = part1 + "http://www.example.com/" + part2;
link.attr("onclick", newOnClick);
There are other ways to inject a string constant into existing string, but this should work - if I understood you well enough.

Categories

Resources