Hi Ov3rfly,
could you provide a link to a page where this is being used (with the 3.x version for example) so I can get a better understanding of it?
I’m sure we can figure something out.
Cheers,
JP
Client does not allow a link, sorry.
Currently a hash like http://example.com/page-url/#slide-5
is used to provide a deeplink which shows slide #5 at page-load. The hash is changed with each slider change.
With 3.x we can (after hash-change) push the current url including the hash to all share-buttons by changing the data('url')
once and re-initialize Shariff, similar to this pseudocode:
$('.shariff').each(function() {
$(this).data('url', our_new_url_with_hash_and_urlencoded);
if (typeof Shariff === 'function') {
this.shariff = new Shariff(this);
}
});
This sets the correct url for all possible params to services like u=
, url=
etc., same for data('title')
which after re-initialize is set to correct text=
, title=
, subject=
, etc.
With 4.x we have to find/replace all the different params one by one.
Got it. Stick with 3.4.2 for the moment. I’ll see how we can incorporate something like this in the next 4.x release.
JP
Thanks for quick feedback, we have updated already to 4.x.
As temporary solution we added custom code which stores the initial href per service and after hash-change replaces/enhances in a .shariff ul li a
loop all (currently known) fields if they are found (currently using http://stackoverflow.com/a/901144 as quick hack, there actually are more efficient solutions) based on the stored href.
This will of course break if additional services / params are available after a plugin update but it works for now.
For interested readers, above mentioned custom code:
// for Shariff Wrapper 4.0.3+
url = new_url_with_hash...
title = new_title..
media = new_media_url..
$('.shariff ul li a').each(function() {
if (!$(this).data('full')) { // first call, store for later...
$(this).data('full', $(this).attr('href'));
}
full = $(this).data('full');
var href_without_hash = window.location.href.replace(window.location.hash, '');
// replace url, without reading params
full = full.replace(encodeURIComponent(href_without_hash), encodeURIComponent(url));
var whatsapp = (full.indexOf('whatsapp:')==0);
var threema = (full.indexOf('threema:')==0);
// replace title, are there other fields?
if (title) {
$.each(['text', 'subject', 'description', 'title'], function(index, val) {
var txt = getParameterByName(val, full);
if (whatsapp || threema) {
txt = txt.substring(0, txt.lastIndexOf(' '));
}
if (txt) {
full = full.replace(val + '=' + encodeURIComponent(txt), val + '=' + encodeURIComponent(title));
return false; // we do only one replace, leave .each() now
}
});
}
// replace media, are there other fields?
if (media) {
$.each(['media'], function(index, val) {
var img = getParameterByName(val, full);
if (img) {
full = full.replace(val + '=' + encodeURIComponent(img), val + '=' + encodeURIComponent(media));
return false; // we do only one replace, leave .each() now
}
});
}
$(this).attr('href', full);
});
Thank you for sharing that with us!