Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript向URL添加参数(Adding a parameter to the URL with JavaScript)

In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:(在使用AJAX调用的Web应用程序中,我需要提交一个请求,但要在URL的末尾添加一个参数,例如:)

Original URL:(原始网址:) http://server/myapp.php?id=10(http://server/myapp.php?id = 10) Resulting URL:(结果网址:) http://server/myapp.php?id=10 &enabled=true(http://server/myapp.php?id = 10 &enabled = true) Looking for a JavaScript function which parses the URL looking at each parameter, then adds the new parameter or updates the value if one already exists.(寻找一种JavaScript函数,该函数解析URL并查看每个参数,然后添加新参数或更新值(如果已经存在)。)   ask by Lessan Vaezi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use one of these:(您可以使用以下之一:)

https://developer.mozilla.org/en-US/docs/Web/API/URL(https://developer.mozilla.org/zh-CN/docs/Web/API/URL) https://developer.mozilla.org/en/docs/Web/API/URLSearchParams(https://developer.mozilla.org/en/docs/Web/API/URLSearchParams) Example:(例:) var url = new URL("http://foo.bar/?x=1&y=2"); // If your expected result is "http://foo.bar/?x=1&y=2&x=42" url.searchParams.append('x', 42); // If your expected result is "http://foo.bar/?x=42&y=2" url.searchParams.set('x', 42);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...