js 修改url后面的参数

https://img1.mukewang.com/5bcc4bc1000121dc02520031.jpg

怎么修改后面的参数

慕仙森
浏览 1946回答 1
1回答

犯罪嫌疑人X

/** url 目标url* param 需要替换的参数名称* paramVal 替换后的参数的值* 返回值为新的url* example: updateURLParameter('www.baidu.com?id=12','id','13') 返回结果为 'www.baidu.com?id=13'*/function updateURLParameter(url, param, paramVal){&nbsp; &nbsp; var newAdditionalURL = "";&nbsp; &nbsp; var tempArray = url.split("?");&nbsp; &nbsp; var baseURL = tempArray[0];&nbsp; &nbsp; var additionalURL = tempArray[1];&nbsp; &nbsp; var temp = "";&nbsp; &nbsp; if (additionalURL) {&nbsp; &nbsp; &nbsp; &nbsp; tempArray = additionalURL.split("&");&nbsp; &nbsp; &nbsp; &nbsp; for (i=0; i<tempArray.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(tempArray[i].split('=')[0] != param){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAdditionalURL += temp + tempArray[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = "&";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; var rows_txt = temp + "" + param + "=" + paramVal;&nbsp; &nbsp; return baseURL + "?" + newAdditionalURL + rows_txt;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript