使用jQuery删除部分URL

Remove part of URL with jQuery

本文关键字:URL 删除部 jQuery 使用      更新时间:2023-09-26

当URL的一部分是可变的时,我该如何使用jQuery删除该部分?

例如,我如何将其从我的URL中删除:?modal=名称

当"name"可能是name1name2名称3

我想我需要拆分URL,但当模式名称可能是任何东西时,我该怎么做呢?

var url = 'http://yourdomain.com/search?modal=name';
alert(url.substring(0, url.indexOf('?')));

演示

正如您所问的"如何使用jQuery 删除URL的一部分"

这里有一个基本的解决方法:

//the URL ( decode it just for GOOD practice)
var someRandomUrl = decodeURI("http://example.com?modal=name");
//here we do the splitting
var splittedParts = someRandomUrl.split("?");
// the first part of the array will be URL you will require
var theURL = splittedParts[0];
// the second part of the array will be the Query String
var theQuery = splittedParts[1];
alert(theURL);