Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove 'http://' from a URL in JavaScript [duplicate]

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.

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=....

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=...

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.

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)+ 

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

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

like image 459
Peter Avatar asked Nov 21 '11 01:11

Peter


People also ask

How do I remove http and www from url?

To remove http:// or https:// from a url, call the replace() method with the following regular expression - /^https?:\/\// and an empty string as parameters. The replace method will return a new string, where the http:// part is removed. Copied!

How do you remove something from a url?

To request removal of a directory or site, click on the site in question, then go to Site configuration > Crawler access > Remove URL. If you enter the root of your site as the URL you want to remove, you'll be asked to confirm that you want to remove the entire site.

How do I remove the last url?

First you need to parse the tag. Next try to extract st_url value which is your url. Then use a loop from the last character of the extracted url and omit them until you see a '/'. This is how you should extract what you want.


2 Answers

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

result = url.replace(/(^\w+:|^)\/\//, ''); 
like image 103
FailedDev Avatar answered Oct 15 '22 09:10

FailedDev


url = url.replace(/^https?:\/\//, '') 
like image 39
matsko Avatar answered Oct 15 '22 09:10

matsko