URL参数hash js的打印值

我正在尝试使用 js 从 URL 获取参数我有一个 url:

http://www.example.com?i=aGVsbG8gd29ybGQ=

我想使用 javascript 解码 base64 ?i 值并在此处打印解码值

<input id="i" type="hidden" value="decode value" />


qq_花开花谢_0
浏览 220回答 4
4回答

哆啦的时光机

尝试这个 :var parameterValue = atob(window.location.search.match(/(\?|&)i\=([^&]*)/)[2])console.log(parameterValue);//"hello world"document.getElementById('i').value=parameterValue;这也可能有帮助: https ://stackoverflow.com/a/26995016/957026

白衣非少年

是的,您可以从 url 获取值并对其进行解码。使用下面的代码function getUrlVars() {&nbsp; &nbsp; var vars = {};&nbsp; &nbsp; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {&nbsp; &nbsp; &nbsp; &nbsp; vars[key] = value;&nbsp; &nbsp; });&nbsp; &nbsp; return vars;}let x = atob(getUrlVars().i);console.log(x); // hello worlddocument.getElementById('i').value = x;

眼眸繁星

你可以使用 window.location API&nbsp;https://developer.mozilla.org/en-US/docs/Web/API/Locationconsole.log(window.location.search);

慕哥9229398

为了解析 URL 字符串,现代浏览器提供了一个名为的类URLSearchParams,这是提取 URL 值的最简单方法。您可以通过传递该类的search属性window.location(在删除初始“?”之后)创建该类的实例,然后它将为您完成所有工作:// eg. https://example.com/?name=Jonathan&age=18&i=aGVsbG8gd29ybGQ=const params = new URLSearchParams(window.location.search.substring(1)); // remove "?"const name = params.get("name"); // is the string "Jonathan"const age = parseFloat(params.get("age")); // is the number 18const i = whateverDecodingFunctionYouUse(params.get("i")); // decoded aGVsbG8gd29ybGQ请参阅:https ://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript