在jQuery中获取url名称并重定向

在jQuery中获取url名称并重定向,jquery,Jquery,我正在使用下面的代码获取jQuery中的当前URL var pathname = window.location.pathname; console.log(pathname); 现在我有了这样的网址 http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html var str = "http://localhost/TantraProjects/CollectiveCr

我正在使用下面的代码获取jQuery中的当前URL

var pathname = window.location.pathname;
console.log(pathname);
现在我有了这样的网址

http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html
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);
我想把URL转换成这个

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

有没有办法像上面那样转换成URL,不管URL是什么?

这并不理想,但你可以这样做

http://localhost/TantraProjects/CollectiveCraft/Repo/WebSite/index.php/crafts/t-lights.html
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);

演示:

假设您的url中总是包含index.php,并且您希望删除index.php之后的所有内容,下面的代码将起作用:

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);
试试这个

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

尝试以下方法

var regex=/(.+\.php)\/(.+)/
var matches=regex.exec(url);//matches[1] contains the result

使用JS/jQuery重定向页面: