I have a php file which is been used to share posts of my users on Facebook and Twitter. If I click on the Twitter icon for example, I get a screen where the hyperlink to my site is automatically been written. This is good, but I like to remove the www.
part of the url that is automatically shown on the Twitter screen. I guess this has something to do with location.host, so I googled, but I couldn't find a proper answer to this problem.
Here is an example of my code:
function e(f,i,an)
{
with(document)
{
write('<div style="width:100%"><table id=m'+i+' style="visibility:hidden"><tr>');
write('<td id="vst' + i + '" style="white-space:nowrap"> </td>');
write('<td hr(this.childNodes[0])" title="share on facebook"><a href="javascript:od(\'http://www.facebook.com/sharer.php?u=' + location.host + '/e/'+ i +'\',900,400)" class=icon><img src="images/facebook.png" target="_blank"></a></td>');
write('<td hr(this.childNodes[0])" title="share on twitter"><a href="javascript:od(\'http://twitter.com/home?status=' + location.host + '/e/'+ i +' - %20read!\',800,600)" class=icon><img src="images/twitter.png" target="_blank"></a></td>');
write('<td class=ei><a name="cid'+i+'"></a><a href="e/'+i+'">( #'+i+' )</a></td>');
document.write('<td> </td>');
My second question would be this: how can I add a custom title on the Twitter screen: for example, my websitename without having to use location.host together with the title of the TOPICNAME that is being shared?
host: This property also returns the same hostname but it also includes the port number. In case, if the port number is not available, then it will just return the hostname.
The window.location.hostname property returns the name of the internet host (of the current page).
hostname. The hostname property of the Location interface is a string containing the domain of the URL.
hostname is the host name (without the port number or square brackets) host is the host name and port number.
For your first... you could modify the host:
location.host.replace('www.','')
Edit: address concerns
Having been down-voted again, and seeing lots of up-votes on the first comment, I will attempt to address concern about subdomains besides www
that contain www
...
Still steering clear of regex for this solution, mostly because generally it is harder to maintain regex, and there are a lot of developers who just don't touch regex at all...
var cleaned_host;
if(location.host.indexOf('www.') === 0){
cleaned_host = location.host.replace('www.','');
}
// do something with `cleaned_host`
... or more succinctly ...
location.host.indexOf('www.') && location.host || location.host.replace('www.', '');
// evaluates to hostname with starting `www.` removed
If you want to get only the second- and top-level-domains, not any subdomains, this should help you:
var url = location.host; // e.g. "www.example.com"
return url.split(".").slice(-2).join("."); // "example.com"
This also works for other subdomains and even for more-than-threeleveldomains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With