Copy DOM content to the clipboard with pure Javascript
Thu, Feb 15 2018, 09:58 Javascript, jQuery, programming, RapidWeaver, Webserver PermalinkOf all the tips on the internet on how to copy data from a DOM element to the clipboard, I compiled my own function(s).
The examples below are to copy the 'href'-part of a link to an RSS feed to the clipboard.
1. By class name, use the first found element
2. By id
RSS feed link example with a class and an ID, using FontAwesome icons :
<a class="blog-rss-link" href="https://macvos.blogspot.com/feeds/posts/default" rel="alternate" target="_blank" title="RSS Feed" type="application/rss+xml"><i class="fa fa-rss"> RSS Feed <i class="fa fa-copy" onclick="copyToClipboardCN('blog-rss-link', 'href')" style="cursor: pointer;" title="Copy RSS link to clipboard">
<a id="blog-rss-link" href="https://macvos.blogspot.com/feeds/posts/default" rel="alternate" target="_blank" title="RSS Feed" type="application/rss+xml"><i class="fa fa-rss"> RSS Feed <i class="fa fa-copy" onclick="copyToClipboardID('blog-rss-link', 'href')" style="cursor: pointer;" title="Copy RSS link to clipboard">
I have thought about combining the two functions into one, but i find that dangerous. In case you don't, here it is:
The examples below are to copy the 'href'-part of a link to an RSS feed to the clipboard.
1. By class name, use the first found element
function copyToClipboardCN(element, attr) { x = document.getElementsByClassName(element); if(x !== undefined && x !== null && x[0] !== undefined) { var textarea = document.createElement("textarea"); document.body.appendChild(textarea); textarea.value = x[0][attr]; textarea.select(); var status = document.execCommand('copy'); textarea.remove(); } }
2. By id
function copyToClipboardID(element, attr) { x = document.getElementById(element); if(x !== undefined && x !== null) { var textarea = document.createElement("textarea"); document.body.appendChild(textarea); textarea.value = x[attr]; textarea.select(); var status = document.execCommand('copy'); textarea.remove(); } }
RSS feed link example with a class and an ID, using FontAwesome icons :
<a class="blog-rss-link" href="https://macvos.blogspot.com/feeds/posts/default" rel="alternate" target="_blank" title="RSS Feed" type="application/rss+xml"><i class="fa fa-rss"> RSS Feed <i class="fa fa-copy" onclick="copyToClipboardCN('blog-rss-link', 'href')" style="cursor: pointer;" title="Copy RSS link to clipboard">
<a id="blog-rss-link" href="https://macvos.blogspot.com/feeds/posts/default" rel="alternate" target="_blank" title="RSS Feed" type="application/rss+xml"><i class="fa fa-rss"> RSS Feed <i class="fa fa-copy" onclick="copyToClipboardID('blog-rss-link', 'href')" style="cursor: pointer;" title="Copy RSS link to clipboard">
I have thought about combining the two functions into one, but i find that dangerous. In case you don't, here it is:
function copyToClipboard(element, attr) { var x = document.getElementById(element); if(x === undefined || x === null) { x = document.getElementsByClassName(element); if(x !== undefined && x !== null && x[0] !== undefined) { x = x[0]; } else { x = undefined; } } if(x !== undefined) { var textarea = document.createElement("textarea"); document.body.appendChild(textarea); textarea.value = x[attr]; textarea.select(); var status = document.execCommand('copy'); textarea.remove(); } }
Comments
Add or Subtract Business Days in Javascript, PHP and Lasso
Tue, Jul 19 2016, 16:34 Javascript, Lasso, PHP, programming, software, Webserver PermalinkI was lately looking for a working version of a function to add and/or subtract business days. The scripts and code I found via Google somehow did not produce the correct results all the time - and I do not mean the loops that simply add a day and check for weekends. Read more . . .
Leer jezelf programmeren
Tue, Dec 02 2014, 14:12 books, Javascript, programming, Projectmanagement, software PermalinkIk zag dat mijn ooit geschreven boeken en artikelen nog niet op de site stonden. Lang geleden heb ik een poging ondernomen het boek 'Leer jezelf programmeren' uit te geven, is niet gelukt: het was te algemeen. Nu dus gratis te downloaden!
Klik hier voor de eerste twee publicaties…
Ik zal eens kijken wat ik nog meer aan interessante artikelen heb liggen die te 'veralgemeniseren' zijn, zodat u ze kunt gebruiken in uw projecten.
Klik hier voor de eerste twee publicaties…
Ik zal eens kijken wat ik nog meer aan interessante artikelen heb liggen die te 'veralgemeniseren' zijn, zodat u ze kunt gebruiken in uw projecten.
Hex2Str and Str2Hex Javascript Functions
Thu, May 30 2013, 16:06 Javascript, programming PermalinkToday I needed some Javascript to convert a HEX-encoded string to an ASCII-encoded string. The reason is that to be able to send all possible strings via AJAX-calls back to the client, without problems with quotes, apostrophes and other non-ASCII7 characters, I decided to encode the AJAX-data to hex, which is a nice and simple ASCII7 string of only the characters 0 through 9 and A through F.
After Googling a bit I found an example on webdeveloper.com and some tips on stackoverflow.com, and both combined with some of my own JS-knowledge, I came up with the following small and handy functions:
For example, after a user made a selection from the autocomplete menu and you return a HEX-encoded string containing multiple values, separated by "+++", to fill other form-fields too:
Or when you return multiple values as a HEX-encoded JSON array:
Happy coding!
After Googling a bit I found an example on webdeveloper.com and some tips on stackoverflow.com, and both combined with some of my own JS-knowledge, I came up with the following small and handy functions:
function Str2Hex(tmp) {
var str = "";
for (var i=0; i<tmp.length; i++)
str += ("00" + (tmp.charCodeAt(i)).toString(16)).substr(-2);
return(str);
}
function Hex2Str(tmp) {
var str = "";
for (var i=0; i<tmp.length; i+=2)
str += String.fromCharCode(parseInt(tmp.substr(i,2),16));
return(str);
}
For example, after a user made a selection from the autocomplete menu and you return a HEX-encoded string containing multiple values, separated by "+++", to fill other form-fields too:
..., select: function(e,u) {
var x = Hex2Str(u.item.value).split("+++");
$("#field1").val(x[0]).change();
$("#field2").val(x[1]).change();
... etc.
return false; } ....
Or when you return multiple values as a HEX-encoded JSON array:
..., select: function(e,u) {
var x = u.item.value;
$("#field1").val(Hex2Str(x.field1)).change();
$("#field2").val(Hex2Str(x.field2)).change();
... etc.
return false; } ....
Happy coding!
Yourhead Collage 2 not working in IE9
Sat, Mar 02 2013, 01:14 Javascript, Photo, programming, RapidWeaver PermalinkI found the bug that blocked YourHead's Collage 2 from working in Internet Explorer 9. You can read about it at, and download the corrected plug-in from, the YourHead Support Forum. I attached it there to my last response. If you can't download it from there, contact me and I'll mail you the 2MB zipped file.
Do not 'install' the unzipped file. Rather copy it to /Users/YOURNAME/Library/Application Support/RapidWeaver/
You can open your 'Library' folder, if you do not see it, like described here: https://www.macworld.com/article/1161156/view_library_folder_in_lion.html
Do not 'install' the unzipped file. Rather copy it to /Users/YOURNAME/Library/Application Support/RapidWeaver/
You can open your 'Library' folder, if you do not see it, like described here: https://www.macworld.com/article/1161156/view_library_folder_in_lion.html
Restrict Lasso AJAX-file calls to the intended web page
Mon, Jan 07 2013, 09:21 AJAX, Javascript, jQuery, Lasso, programming, Webserver PermalinkSuppose you have a nice setup where a page interacts with the server via AJAX-calls and executes a Lasso file on the server to get some data. You don't want this file to be called directly via the URL-bar in a web browser, or via other self-made web pages by others who try to access it via a copy of your page. Anybody can see which AJAX-files your page is calling, so for some it is always a challenge to execute them outside the normal webpage to see what data will come up. Might be of interest! So you want to prevent that, somehow.
There is a Lasso-tag called referrer_url, which returns a string containing the URL that requested your AJAX-page. If you look into this string for a domain name or a path that only you have, you can block execution if the requestor is not coming from your server. When a page is called directly in the browser, the referrer_url is always an empty string. Which is logical, since the page was not referred to by another page.
Suppose I have a page mypage.html with a jQuery auto-complete implementation in it. This auto-complete can of course be used by more than one page and you do not want people to try it out in other ways.
...
...
<input type="text" id="inp1" size="25"><span id="desc1"></span>
...
...
<script>
$(document).ready(function() {
$("#inp1").autocomplete({minLength:2, source: "ajax.lasso?p1=a&p2=b", select: function(e,u) { $("#inp1").val(u.item.value); $("#desc1").html((u.item.label).replace("(" + u.item.value + ")", "")); return false; } });
});
</script>
Simple protection:
[
if (referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html');
...
...
/if;
]
Better protection:
[
if (string(referrer_url)->beginswith('http://my.domain.com/') &&
(referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html'));
...
...
/if;
]
So this gives you some protection from just try something-users. Add a login-system, which restricts the number of users that might want to hack your pages - you can trace their actions on your site. In that case, add a check if the user is logged in. You must execute your complete login-sequence in your AJAX-pages too, as with 'normal' pages, since the xhttprequest is a normal HTTP request and thus the browser sends the same HTTP-headers and cookies, etc.. to your AJAX-page.
More protection:
[
if (referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html');
var('loggedIn = false');
include('checkuser.lasso');
if($loggedIn);
...
...
/if;
/if;
]
Even better protection:
[
if (string(referrer_url)->beginswith('http://my.domain.com/') &&
(referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html'));
var('loggedIn = false');
include('checkuser.lasso');
if($loggedIn);
...
...
/if;
/if;
]
But, as with everything web-related, nothing can be trusted.
There is a Lasso-tag called referrer_url, which returns a string containing the URL that requested your AJAX-page. If you look into this string for a domain name or a path that only you have, you can block execution if the requestor is not coming from your server. When a page is called directly in the browser, the referrer_url is always an empty string. Which is logical, since the page was not referred to by another page.
Suppose I have a page mypage.html with a jQuery auto-complete implementation in it. This auto-complete can of course be used by more than one page and you do not want people to try it out in other ways.
...
...
<input type="text" id="inp1" size="25"><span id="desc1"></span>
...
...
<script>
$(document).ready(function() {
$("#inp1").autocomplete({minLength:2, source: "ajax.lasso?p1=a&p2=b", select: function(e,u) { $("#inp1").val(u.item.value); $("#desc1").html((u.item.label).replace("(" + u.item.value + ")", "")); return false; } });
});
</script>
Simple protection:
[
if (referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html');
...
...
/if;
]
Better protection:
[
if (string(referrer_url)->beginswith('http://my.domain.com/') &&
(referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html'));
...
...
/if;
]
So this gives you some protection from just try something-users. Add a login-system, which restricts the number of users that might want to hack your pages - you can trace their actions on your site. In that case, add a check if the user is logged in. You must execute your complete login-sequence in your AJAX-pages too, as with 'normal' pages, since the xhttprequest is a normal HTTP request and thus the browser sends the same HTTP-headers and cookies, etc.. to your AJAX-page.
More protection:
[
if (referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html');
var('loggedIn = false');
include('checkuser.lasso');
if($loggedIn);
...
...
/if;
/if;
]
Even better protection:
[
if (string(referrer_url)->beginswith('http://my.domain.com/') &&
(referrer_url >> '/mypage.html' || referrer_url >> '/myotherpage.html'));
var('loggedIn = false');
include('checkuser.lasso');
if($loggedIn);
...
...
/if;
/if;
]
But, as with everything web-related, nothing can be trusted.
Get Day or Date of the Monday of a given week
Tue, Jan 18 2011, 17:59 Javascript, programming PermalinkI have created a Javascript function to get the date of the monday in a given week. The following functions are included:
- mv_leapck(year, flag) - returns 0 or 1 or 365 or 366,
- mv_isDateObject(something) - returns 0 or 1
- mv_dateToString(something, formatstring) - returns a string containing parts of a date according to a passed format
- mv_mondayOfWeek(year, weekno, flag) - returns the monday of the week as (a) a day number (YYYYDDD) or (b) milliseconds or (c) a date as YYYYMMDD
Use Flot graphing library with Lasso Professional
Thu, Nov 25 2010, 23:05 Javascript, jQuery, Lasso, programming PermalinkI have created a quick example on how to create a graph with Flot and Lasso Professional. Flot is a really cool graphing library and Lasso is a cool programming language. Together, they create a great graph!