Meules
Meules

Reputation: 1389

Problems with a relative url

I have a script which gets a url from a page in a website. Like so:

 var catHref = $(this).closest('[id]').children('a').attr('href');
//outcome is something like http://www.website.com/

After that I use this url to get some Json content. Like so:

 var url = catHref;

  $.getJSON(url, function (json){     
      ... etc ...
  }

The problem is that this url is not relative, so it is always http://. Firefox has very strict security. So when I request the Json on a https:// or secured page I always get an error. In Chrome and IE everything works fine. So what I try to do is convert this url to a relative one. Like so:

 var url = '//' + catHref.replace(/^https?:\/\/[a-z_\-\.]+?\.[a-z]{2,5}\//i, '');

Unfortunately this doesn't work. It gives an url like:

https://http//www.website.com/

Does anyone know how to do this properly?

Upvotes: 0

Views: 42

Answers (1)

Mooseman
Mooseman

Reputation: 18891

url2 = url.split("/");
if((url2[0]==="http:")&&(window.location.href.split("/")[0]==="https:")){
    url2[0] = "https:";
    url = url2.join("/");
}

Fiddle: http://jsfiddle.net/Sj4td/

Upvotes: 1

Related Questions