使用javascript或jquery删除url参数 [英] remove url parameters with javascript or jquery

查看:746
本文介绍了使用javascript或jquery删除url参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用YouTube数据api来生成视频播放列表。但是,视频网址需要youtube.com/watch?v=3sZOD3xKL0Y的格式,但是api生成的是youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata。所以我需要做的就是能够选择所有后跟包括和号并将其从URL中删除。任何方式使用javascript和某种正则表达式来执行此操作?

I am trying to use the youtube data api to generate a video playlist. However, the video urls require a format of youtube.com/watch?v=3sZOD3xKL0Y, but what the api generates is youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata. So what I need to do is be able to select everything after and including the ampersand and remove it from the url. Any way to do this with javascript and some sort of regular expression?

推荐答案

简单:

var new_url = old_url.substring(0, old_url.indexOf('?'));

修改:
这将从URL中删除所有参数或片段

Modified: this will remove all parameters or fragments from url

var oldURL = [YOUR_URL_TO_REMOVE_PARAMS]
var index = 0;
var newURL = oldURL;
index = oldURL.indexOf('?');
if(index == -1){
    index = oldURL.indexOf('#');
}
if(index != -1){
    newURL = oldURL.substring(0, index);
}

这篇关于使用javascript或jquery删除url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆