lisovaccaro
lisovaccaro

Reputation: 33956

Script inside href how to

I'm using this link:

<a href="javascript:location='http://www.linkimprov.com/?ref='+encodeURI(location.href).substring(7);"></a>

It is intended for use as a bookmark. When clicked it redirects the user to an url that takes the previous location removing the 7 first characters from it.

Instead of removing the first 7 characters from '+encodeURI(location.href)' I want it to do this:

if(encodeURI(location.href).match(/http:\/\//))
{
encodeURI(location.href).substring(7);
}    
if(encodeURI(location.href).match(/https:\/\//))
{
encodeURI(location.href).substring(8);
}
if(encodeURI(location.href).match(/^www\./))
{
encodeURI(location.href).substring(4);
}

How could make this work inside the href?

thanks

Upvotes: 0

Views: 2942

Answers (1)

qwertymk
qwertymk

Reputation: 35274

How about something like this:

location = 'http://www.linkimprov.com/?ref=' + encodeURI(
    location.href.match(/(?=https?:\/\/)?(?=www\.)?(.*)/)[1]
).substring(7);

Or in a link:

<a href="javascript:location='http://www.linkimprov.com/?ref='+encodeURI(location.href.match(/(?=https?:\/\/)?(?=www\.)?(.*)/)[1]).substring(7);"></a>

EDIT: Try this:

 location = 'http://www.linkimprov.com/?ref=' + encodeURI(
    location.href.match(/^(https?:\/\/)?(www\.)?(.*)/).pop()
).substring(7);

Upvotes: 2

Related Questions