Make JavaScript link open in new window with document.write - javascript

I'm to figure out how to code so that the link opens into a new window? Is this the correct way to do that?
<script>
var copyright = "© ";
var d = new Date();
var n = d.getFullYear();
var db = " Doing Business";
var str = new String(" Link");
document.write(copyright + n + str.link("https://www.google.com"));
document.write(db);
</script>

Please stop using String.link. It's old, deprecated, and smelly. It also doesn't support what you are trying to do.
You can construct a link with the appropriate target attribute (read more) to open in a new tab as follows:
var myLink = "<a target='_blank' href='https://www.google.com'>Link</a>";
document.write(copyright + n + " " + myLink);

Related

Alfresco - Calendar event using server-side javascript

How to create calendar event using server-side javascript code?
Here is the code for creating calendar event.
var node = companyhome.childByNamePath("Sites/demo/calendar");
var myEvent = node.createNode(new Date().getTime() + "-" + Math.round(Math.random()*10000) + ".ics", "ia:calendarEvent")
myEvent.properties["ia:whereEvent"] = "Where event";
myEvent.properties["ia:descriptionEvent"] = "This is the description";
myEvent.properties["ia:whatEvent"] = "What event";
var fromDate = new Date();
var fromISODate = utils.toISO8601(fromDate);
myEvent.properties["ia:fromDate"] = fromISODate;
var toDate = new Date();
toDate.setHours(toDate.getHours() + 3);
var toISODate = utils.toISO8601(toDate);
myEvent.properties["ia:toDate"] = toISODate;
myEvent.save();
logger.warn("Created new calendar event: " + myEvent.nodeRef);

Is there a reason why we might use a function over an href tag?

I am working on a project and have been shown a method where we use a function to open a new window. The code looks like this:
Javascript:
function createWindow() {
let url = "https://google.com";
let win = window.open(url, "My New Window", "width=300, height=200");
document.getElementById("result").innerHTML =
win.name + " - " + win.opener.location;
}
let h = window.outerHeight
let w = window.outerWidth
window.document.write("Height and width of the window are: " + h + " and " + w)
HTML:
<h3>Click to open a new Window:</h3>
<button onclick="createWindow()">Open a Window</button>
<p id="result"></p>
is there a reason why we would use this over using a button with an anchor tag and a target tab?

Close Browser tab if PDF has expired

It works fine when document is opened in Acrobat, but does not work properly when the same PDF document is opened in web browser such as Google Chrome. this.closeDoc() is not getting executed in the Browser.
string path2 = #"D:\test\input.pdf";
string output = #"D:\test\output.pdf";
if (File.Exists(path2))
{
iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(output);
iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfReader(path2) ,writer);
pdfDocument.AddNewPage();
String js = "var rightNow = new Date();"
+ "var endDate = new Date('2017-07-13');"
+ "if(rightNow.getTime() > endDate){"
+ "app.alert('This Document has expired, please contact us for a new one.',1);"
+ "this.closeDoc();}"
+ "else{}";
pdfDocument.GetCatalog().SetOpenAction(iText.Kernel.Pdf.Action.PdfAction.CreateJavaScript(js));
pdfDocument.Close();
}
Are you try "window.close()" ?
String js = "var rightNow = new Date();"
+ "var endDate = new Date('2017-07-13');"
+ "if(rightNow.getTime() > endDate){"
+ " app.alert('This Document has expired, please contact us for a new one.',1);"
+ " this.closeDoc(); "
+ " if(window){ window.close(); } "
+ "}"
+ "else{}";

Cookie is not being read by Edge or Safari

I'm having a problem where my cookies are not being read by Safari or MS Edge. It works fine in all the other browsers just not in these 2.
Has anyone come across a similar issue? If so how did you manage to sort it out? Thank you.
$('.primary-button.eligibility-button').mousedown(function() {
var selectedCard = $('input[name="cardInterestOption"]:checked').next().text();
var balanceTransferOption = $('input[name="balanceTransferOption"]:checked').next().text().replace(/[a-z]/g,'');
var annualIncome = parseFloat($( "input[name='annualIncome']").val());
var otherIncome = parseFloat($( "input[name='otherIncome']").val());
var totalIncome = annualIncome + otherIncome;
document.cookie = "creditCardSelectorEligibility=" + selectedCard + "; path=/;";
document.cookie = "creditCardBalanceTransfer=" + balanceTransferOption + "; path=/;";
document.cookie = "creditCardTotalIncome=" + totalIncome + "; path=/;";
});
(function() {
var page = "";
var percentage = "NA";
var path = location.pathname;
if(/results/.test(path)){
page = "Success";
var results = "";
var percent = $(".results-banner:eq(0)").text();
if(/\b[0-9]{2}/.test(percent)){
results = percent.match(/\b[0-9]{2}/)[0]
}
}
else if(/verify/.test(path)){
page = "Verify";
}
else if(/problem/.test(path)){
page = "Problem";
}
else if(/noteligible/.test(path)){
page = "Not Eligible";
}
else if(/unavailable/.test(path)){
page = "Unavailable";
}
var selectedCard = bt_cookie('yrd_creditCardSelectorEligibility')
var balanceTransferOption = bt_cookie('yrd_creditCardBalanceTransfer')
var totalIncome = bt_cookie('yrd_creditCardTotalIncome')
return page + "|" + selectedCard + "|" + balanceTransferOption + "|" + totalIncome + "|" + results;
})();
What we have found happen sometimes, is that the page loads too quick for the cookie to be created. So we have had to change the way the Cookies get created. For instance you can change the code to create the Cookie as the user interacts with the form. ie: If the select a button or fill in a field. Capture the cookie there, rather than at the end of the page where the customer has to click the submit. Hopefully this makes sense?

Html tables within div export to excel

<script type="text/javascript">
$(document).ready(function () {
//getting values of current time for generating the file name
$(".toExcelButton").click(function(){
var dt = new Date();
var day = dt.getDate();
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
var hour = dt.getHours();
var mins = dt.getMinutes();
var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = 'exported_table_' + postfix + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
})
});
</script>
Need to export div tables to excel. The above code works fine in Chrome but not working in IE. Can anyone help me out on the same.
In IE a dynamically created anchor tag needs to be added to the DOM to execute its click event. Furthermore the download attribute is not supported in the IE:
Download attribute on A tag not working in IE
Edit:
Recently I posted many answers handling this issue, here are two of those:
image does not download with it's own extension
JS Base64 string to downloadable pdf - Edge
Basically you have to use msSaveOrOpenBlob() in IE:
var tF = 'Whatever.xls';
var tB = new Blob(..);
if(window.top.navigator.msSaveOrOpenBlob){
//Store Blob in IE
window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
//Store Blob in others
var tA = document.body.appendChild(document.createElement('a'));
tA.href = URL.createObjectURL(tB);
tA.download = tF;
tA.style.display = 'none';
tA.click();
tA.parentNode.removeChild(tA)
}
In the case above:
var tT = new XMLSerializer().serializeToString(document.querySelector('table')); //Serialised table
var tF = 'Whatever.xls'; //Filename
var tB = new Blob([tT]); //Blob
if(window.top.navigator.msSaveOrOpenBlob){
//Store Blob in IE
window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
//Store Blob in others
var tA = document.body.appendChild(document.createElement('a'));
tA.href = URL.createObjectURL(tB);
tA.download = tF;
tA.style.display = 'none';
tA.click();
tA.parentNode.removeChild(tA)
}
https://jsfiddle.net/23ao1v0s/1/
Please check the below given link. I think you will get a solution for your question
https://github.com/rainabba/jquery-table2excel

Categories

Resources