strip characters from url on javascript 'onclick'-command

こ雲淡風輕ζ 提交于 2020-01-14 06:04:53

问题


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>


来源:https://stackoverflow.com/questions/8782120/strip-characters-from-url-on-javascript-onclick-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!