1

I am using the below code for getting the current URL in jQuery.

var pathname = window.location.pathname;
console.log(pathname);

Now I have got the url like this

http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html

I want to convert the URL into this

http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php

Is there any a way to convert into URL as above, whatever the URL is ?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Ranjit
  • 1,684
  • 9
  • 29
  • 61
  • Redirecting the page using JS/jQuery: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Hashem Qolami Aug 08 '13 at 11:17

4 Answers4

2

Its not ideal but you can something like this

var str = "http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html";

var modUrl = str.substring(0, str.indexOf("php") + 3);
alert(modUrl);

Demo: http://jsfiddle.net/JmLq9/

Satpal
  • 132,252
  • 13
  • 159
  • 168
1

Assuming you will always get index.php in your url and you want to remove everything after index.php, following code will work:

var url='http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html';
var b= url.split('index.php');
var newurl=b[0]+'index.php';
document.write(newurl);
shairya
  • 173
  • 9
0

Try this,

var url = "http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html";
var ind = url.replace(url.split('.php')[1],'');
alert(ind);

Fiddle: http://jsfiddle.net/VhbA4/

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Try the following

var regex=/(.+\.php)\/(.+)/
var matches=regex.exec(url);//matches[1] contains the result
SoWhat
  • 5,564
  • 2
  • 28
  • 59