Question

If clarification is needed, please let me know. If it can't be done, please let me know this as well. I am desperately trying to figure this out still

I was following Google's Dev guide to the Share button at the following site:

https://developers.google.com/+/web/share/

and I can not seem to figure out how, if it's even possible, to use a custom icon AND use the current URL instead of having to specify a URL.

I found this section of their site that specifies an anchor tag address:

"https://plus.google.com/share?url={URL}"

This would allow me to use a custom icon (and the only way I can use a custom icon as far as I can tell) and a few other custom parameters as well. But it looks like this method requires a specified URL and, as far as I can tell, provides no method to dynamically create the link depending on the current page.

If I use the code generator at the top, it will use the current page, but it calls on a Google hosted Java Script and in addition, it is a hover link that pops up when I hover over the icon. And of course, I also can't use a custom icon with the generator either.

I've been Googling every search term I could think of and searching this site as well and I haven't been able to find anyone else asking this question as of yet. I figured after about 20-30 minutes of searching that I wasn't going to find my answer via searching, so i apologize if this has been answered.

Just some background on my experience to give an idea of where I sit: I have a decent grasp of the workings of HTML and CSS. Javascript, however, I understand very very basic theory and that's about it. I definitely intend to learn, however, as it will prove a very valuable skill.

Thank you very much!!

I think I know what needs to be done, but...I don't know how to do it (or if it would even work) :|

my share link needs to link to a script that looks at the current page's URL, and then takes that information, and creates a dynamic link from it that will take the user to the following link: https://plus.google.com/share?url={URL from query will be here}.

I think that might work...it sounds like it would. Any thoughts? If so, any simple scripts around that would do just this?

Double thanks!!

--I finally found something that works, but it uses Javascript and I don't fully understand it, just enough to tweak it. It took me forever to find this, but it works with Google Plus, Facebook, or Twitter! (and I'm sure it will work with any other website that supplies a Share Link that requires a specified URL)

Here it is, I'm still looking for a better solution, but this does exactly what I was looking for:

<a href="javascript:(
function(){
var w=480;var h=380;
var x=Number((window.screen.width-w)/2);
var y=Number((window.screen.height-h)/2);
window.open('https://plus.google.com/share?url='+encodeURIComponent(location.href)+'
  &title='+encodeURIComponent(document.title),'','width='+w+',height='+h+',left='+x+',top='+y    +',
  scrollbars=no');
})();" style="background: url(/wp-content/themes/HTML5/images/googleplus.png) no-repeat  scroll left center transparent;">
Share to Google+</a>
Was it helpful?

Solution

EDIT! After spending some months learning Javascript, I've built a solution that is much better than that which is provided below. I'll leave my original answer, however, I want to place this better solution at the top.

This solution should work on ANY social media platform that gives you a custom share URL (that is to say, a url that allows you to manually type in an address to share).

Here is how it all works (and if anyone has any suggestions or tweaks that have more experience with JS, please let me know).

  1. I assign variables to the document.URL and document.titleproperties.
  2. I write a named function (I called mine, socialShare) that is set to run via an anonymous function on the window.onloadevent.
  3. The socialShare function assigns variables to the location of my social button's within the HTML. In my case, I used IDs to locate the elements. The purpose of these variables is purely for aesthetics (I use these variables to re-write the the HTML code dynamically, so that when you hover over the share button, it displays the correct URL for sharing the current page you are on)
    • var fbShare = document.getElementById("fbShare");
    • var gplusShare = document.getElementById("gplusShare");
    • twitterShare = document.getElementById("twitterShare");
  4. I then write three separate anonymous functions, one for each social media platform. Each function has two statements. The functions work as follows: the first part is the variable assigned to the location of the HTML element with the ID fbShare. The second part tells it to run the function when that element is clicked; .onclick. The third part is the anonymous function that will run when that element is clicked. The first statement of this function will open a new window; window.open; and in that new window, it will open the URL that is specified by feeding the window.open method parameters. The parameters are as follows (URL,name,specs) where URL is the URL you want to share, name is optional and left blank as seen by the empty set of quotes, and finally specs is where you specify attributes of the window (IE: width and height). The first parameter, the URL: ("https://www.facebook.com/sharer.php?u="+currentURL, currentURL is the global variable that was assigned earlier and will place whatever the current documents URL is, in place of currentURL. The second parameter, the name: "", This is left blank, as it is optional. The third parameter, the specs: "height=368,width=600,left=100,top=100,menubar=0"); These are a comma-seperated list of items. In my case, I've specified a height, width, and the location of the window, as well as disabled the menubar. Finally, the second statement, return false; tells the browser NOT to follow the link inside the HTML code. If this was not specified, then the browswer would follow the URL in the HTML, AND open a new window. For more information on the window.open method, please see the link at the bottom of this new answer.

    • fbShare.onclick = function() { window.open("https://www.facebook.com/sharer.php?u="+currentURL,"","height=368,width=600,left=100,top=100,menubar=0"); return false; }

    • gplusShare.onclick = function() { window.open("https://plus.google.com/share?url="+currentURL,"","height=550,width=525,left=100,top=100,menubar=0"); return false; }

    • twitterShare.onclick = function() { window.open("https://twitter.com/share?url="+currentURL+"&text="+currentTitle,"","height=260,width=500,left=100,top=100,menubar=0"); return false; }

  5. And finally, I modify the HTML href elements of each social media button so that when the user hovers over the share buttons, they see the correct Share URL displayed in their browsers status bar. The first part of this statement grabs the element id, fbShare and the second part tells it to set an attribute, .setAttribute. Then we pass in the attribute name that we want to change, ("href", in this case, and then we pass in what we would like the new attribute value to be, "http://www.facebook.com/sharer.php?u="+currentURL); currentURL is the same here, as earlier. It is the variable that holds the value for whatever the current page's URL is.

    • fbShare.setAttribute("href","http://www.facebook.com/sharer.php?u="+currentURL);

    • gplusShare.setAttribute("href","https://plus.google.com/share?url="+currentURL);

    • twitterShare.setAttribute("href","https://twitter.com/share?url="+currentURL+"&text="+currentTitle);

That's about all there is to it! I hope I wrote this well and I hope it is relatively easy to follow. If any pros out there have any suggestions, please feel free to toss in and give your advice! :)

My JS file
http://jrltest.host-ed.me/_js/share.js

Link to information on the window.open method at w3schools.com
http://www.w3schools.com/jsref/met_win_open.asp

Link to information on the .setattribute method at w3schools.com
http://www.w3schools.com/jsref/met_element_setattribute.asp



OLD ANSWER: I figured I'd add this as an answer. It does the trick and solves the exact problem that I had. The URL after 'window.open' would be the social media's Share Link (in the case of the example, it's google plus' Share Link. There are a few variables that can be either modified or removed. Anyone that's good with scripting could probably create a PHP version (which I would LOVE) or modify it to better suite their needs. At any rate, I hope this will help someone out!

<a href="javascript:(
function(){
var w=480;var h=380;
var x=Number((window.screen.width-w)/2);
var y=Number((window.screen.height-h)/2);
window.open('https://plus.google.com/share?url='+encodeURIComponent(location.href)+'
  &title='+encodeURIComponent(document.title),'','width='+w+',height='+h+',left='+x+',top='+y+',
  scrollbars=no');
  })();" style="background: url(/wp-content/themes/HTML5/images/googleplus.png) no-repeat scroll left center transparent;">
Share to Google+</a>

OTHER TIPS

Native Window Open function its not a good idea, browsers like Mozilla and Chrome block pop up. I think its better use a plugin to open a new windows with the share url, like jquery popup plugin. Work very fine for me and browser cant block it.

Copy an paste into a new js file like original name: 'jquery.popup.js'

jQuery.fn.popup = function(options) {
var defaults = {
    width: screen.width/2,
    height: screen.height/2,
    titlebar: false,
    status: false,
    resizable: true,
    toolbar: false,
    scrollbars: true,
    menubar: false
};
var options = jQuery.extend(defaults, options);

Boolean.prototype.setProperty = function() {
    if (this == true) { return "yes"; } else { return "no"; }
};

jQuery(this).click( function() {
    var target = this.target;
    var href = this.href;
    var posY = (parseInt(screen.height/2)) - (parseInt(options.height/2));
    var posX = (parseInt(screen.width/2)) - (parseInt(options.width/2));
    var win = window.open(href, target, 'titlebar=' + options.titlebar.setProperty() + ', screenX='+ posX +', screenY='+ posY +', left='+ posX +', top='+ posY +', status=' + options.status.setProperty() + ', resizable=' + options.resizable.setProperty() + ', toolbar=' + options.toolbar.setProperty() + ', scrollbars=' + options.scrollbars.setProperty() + ', menubar=' + options.menubar.setProperty() + ', width='+ options.width +', height='+ options.height);
    win.focus();
    return false;
});
return this;
};    

USAGE:

<script src="jquery.last.js"></script>

<script src="jquery.popup.js"></script>

<script>
jQuery(function(){
 //simple load
 jQuery(".popupLink").popup({ width: 640, height: 480 });
 });
</script>
<a class='popupLink' href="https://www.facebook.com/share.php?u=<?php echo URL;?>">Share Facebook</a>

ALSO YOU CAN PASS OPTIONS LIKE THE PLUGIN EXAMPLE OPTIONS

<script>
jQuery(".popupLink").popup({ width: 640, height: 480, resizable: false, menubar: true });
</script>

The author website dont exist any more. This are the information that comes with the plugin comments

/* * jQuery popup v1 - A jQuery popup plugin. * By Jordan Thomas - http://labs.wondergroup.com * Licensed under the do whatever you want to license. * If you like, keep this message intact so * someone else can find the origin. */

You can also use PHP to resize the window but here is the PHP version... Have fun :) See Also: PHP - Getting Current URL

<?
echo '<a href="https://plus.google.com/share?url='.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'" 
target="_blank"><img src="images/google-custom-icon.png"></a>';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top