如何从JavaScript中的URL中删除“http://” [英] How to remove 'http://' from a URL in JavaScript

查看:173
本文介绍了如何从JavaScript中的URL中删除“http://”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的情况。我正在编写一个JavaScript书签,允许用户轻松快速地点击并共享外部网站到我们的网站。它只是获得标题,页面URL,如果他们在页面上选择了任何文本,它也会抓住它。

I have run into an odd situation. I'm writing a JavaScript bookmarklet that will allow users to click and share external websites to our website very easily and quickly. It simply get's the title, page URL, and if they've selected any text on the page, it grabs it too.

问题是它无效外部域由于某种原因,所以如果我们在内部使用它,我们最终得到一个共享窗口,其URL格式如下:

The problem is it doesn't work with external domains for some reason, so if we use it internally we end up with a share window with the URL formatted like this:

http://internaldomain.com/sharetool.php?shareid=http://internaldomain .com / anotheroddpage.html& title = ....

这很好,但是如果我们尝试使用外部域并最终得到一个格式如下的URL:

That works just fine, BUT if we try to use an external domain and end up with a URL formatted like this:

http://internaldomain.com/sharetool.php?shareid=http://externaldomain.com/coolpagetoshare.html&title= ...

然后我们在p上收到 Forbidden Error 年龄并且无法加载...如果我们从externaldomain地址手动删除 http:// ,它会再次加载。

Then we get a Forbidden Error on our page and can't load it... If we manually remove the http:// from the externaldomain address, it loads just fine again.

所以..我认为解决这个问题的最佳解决方案是修改JavaScript书签,删除 http ,因为它正在加载窗口。以下是我当前书签的外观:

So.. I'm thinking the best solution to get around this problem is to modify the JavaScript bookmarklet to remove the http as it's loading the window. Here is how my current bookmarklet looks:

javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://internaldomain.com/sharetool.php',l=d.location,e=encodeURIComponent,u=f+'?u='+e(l.href)+

如您所见, e(l.href)是传递URL的位置。

As you can see, e(l.href) is where the URL is passed.

如何修改它以删除外部域 http://

How can I modify that so it removes the external domains http://?

推荐答案

我认为考虑所有可能的协议会更好。

I think it would be better to take into account all possible protocols.

result = url.replace(/(^\w+:|^)\/\//, '');

这篇关于如何从JavaScript中的URL中删除“http://”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆