1
if(url.contains(/) at the end of the url){
    //do nothing
}
else if(url.not contains(/) at the end of the url)
    //url must be appended with "/"
}

<a href="www.xyz.com/abc"></a> this is my piece of code

the url here is www.xyz.com/abc but i require it as www.xyz.com/abc/

if u notice the new url, it is appended by "/"

Thanks

Vaandu
  • 4,817
  • 12
  • 47
  • 75
Debjeet Das
  • 23
  • 1
  • 5

6 Answers6

3
var url = "www.xyz.com/abc";
if(url.slice(-1) != "/"){
   url += "/";
}

Demo --> http://jsfiddle.net/mohammadAdil/Zf3gN/2/

Mohammad Adil
  • 44,013
  • 17
  • 87
  • 109
3

regular expressions

if (!str.match("/$")) {
    str += "/";
}

DEMO

Roko C. Buljan
  • 180,066
  • 36
  • 283
  • 292
Niche
  • 977
  • 5
  • 23
1

Try

par = link.indexOf('/') != -1 ? link : link+"/"
Salil
  • 45,112
  • 20
  • 117
  • 151
1

Since you only have to check the last character, you can do exactly that:

var url = 'http://www.bla.com';
if (url.charAt(url.length - 1) != '/') {
  url += '/';
}
Ja͢ck
  • 166,373
  • 34
  • 252
  • 304
  • Better to use `url.charAt(url.length - 1)`. See http://stackoverflow.com/questions/5943726/string-charatx-or-stringx. – jfriend00 May 09 '13 at 07:24
0
url = url.lastIndexOf('/') !== (url.length -1) ? url += '/' : url;

Check fiddle

Sushanth --
  • 54,565
  • 8
  • 62
  • 98
0

try below for adding / at the end of all the links present on page

$('a').each(function() {
    var link = this.href;
    this.href = link.lastIndexOf('/') != (link.length -1)  ? link += '/' : link;
});
Ganesh Bora
  • 1,083
  • 8
  • 16