strip characters from url on javascript 'oncli

2019-06-13 15:34发布

问题:

I'm afraid this may be a very stupid question.

I want to refer people via a pop-up and automatically fetch the url from the current document (so that I don't have to adapt the code to every page).

The link I'm using is like this:

<a href="http://www.facebook.com/sharer.php" title="Add to Facebook" onclick="window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(location.href), 'facebook','toolbar=no,width=550,height=550'); return false;"></a>

The problem I'm facing is with the part that adds the (current) url: +encodeURIComponent(location.href). The url always looks like this:

www.MYDOMAIN.com/SECTION/index.php

For cosmetic reasons I would prefer it to look like this:

www.MYDOMAIN.com/SECTION

In short: is there a way to strip away the last 10 characters of the url within the 'onclick' command? The last 10 characters are always without exception /index.php.

Thank you for your help. I really appreciate any comment on this!

回答1:

add .replace(/\/index.php$/, '')

<a href="http://www.facebook.com/sharer.php" title="Add to Facebook" onclick="window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(location.href.replace(/\/index.php$/, '')), 'facebook','toolbar=no,width=550,height=550'); return false;"></a>