javascript '?'后如何读取地址栏的内容 对 HTML 进行签名和插入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9526310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:58:48  来源:igfitidea点击:

How to read content of the address bar after '?' sign and insert value to HTML?

javascripthtml

提问by HelpNeeder

I am redirecting from some page to index.html. Page redirect to index by using following address in the address bar: http://localhost/index.html?page.html

我正在从某个页面重定向到 index.html。使用地址栏中的以下地址将页面重定向到索引:http://localhost/index.html?page.html

How can I read the value after ?sign and insert it (page.html) into index.html file?

如何在?签名后读取值并将其(page.html)插入到 index.html 文件中?

回答by Joseph

var url = window.location.href;
var params = url.split('?');

alert(params[1]);

回答by Brad Christie

window.location.searchalong with substringto remove the ?

window.location.search随着substring删除?

More information can be found through the link regarding the property, but with regards to printing it on the page:

更多信息可以通过有关该财产的链接找到,但关于将其打印在页面上:

<script type="text/javascript">
  var GET = window.location.search.substring(1);
  document.write(GET);
</script>

回答by elclanrs

You could also extract it with a RegExp. Maybe not ideal but a solution nonetheless:

您也可以使用 RegExp 提取它。也许不理想,但仍然是一个解决方案:

var url = location.href.match(/\?(.+)/)[1];

回答by faino

Split()the document.URLby the lastIndexOfand parse it from there:

Split()document.URLlastIndexOf和解析从那里:

var url=document.URL;
var urls=url.substr(url.lastIndexOf('?')+1,url.length);
console.log(urls);
// urls will contain everything right of the ?