本文介绍了通过javascript'onclick'-command从url中剥离字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我恐怕这可能是一个非常愚蠢的问题。

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

我想通过弹出窗口引用用户,并自动从当前文档中获取url所以我不必将代码适应每一页)。

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>

我面临的问题是添加(当前)url的部分: + encodeURIComponent(location.href)。网址总是如下所示:

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

总之:在'onclick'命令中去掉url的最后10个字符?最后10个字符总是无异常 /index.php

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.

感谢您的帮助。

推荐答案

添加 .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>

这篇关于通过javascript'onclick'-command从url中剥离字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:53