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,857
  • 12
  • 49
  • 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/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
3

regular expressions

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

DEMO

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Niche
  • 967
  • 5
  • 23
1

Try

par = link.indexOf('/') != -1 ? link : link+"/"
Salil
  • 46,566
  • 21
  • 122
  • 156
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
  • 170,779
  • 38
  • 263
  • 309
  • 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 --
  • 55,259
  • 9
  • 66
  • 105
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,133
  • 9
  • 17