我有一个html页面,使用看起来有点像这样的网址加载:
http://localhost:8080/GisProject/MainService?s=C&o=1
我想在不使用jsp
的情况下获取网址中的参数。可以使用Javascript
或jQuery
来完成。我想使用{{1}来测试我的网页本地服务器在将其部署到使用Java服务器的远程计算机之前。
是否有任何图书馆可以让我这样做。
答案 0 :(得分:28)
给出了一个很好的解决方案here:
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
这是你如何使用这个函数假设URL是,
http://dummy.com/?technology=jquery&blog=jquerybyexample
:
var tech = GetURLParameter('technology');
var blog = GetURLParameter('blog');`
答案 1 :(得分:1)
//http://localhost:8080/GisProject/MainService?s=C&o=1
const params = new URLSearchParams(document.location.search);
const s = params.get("s");
const o = params.get("o");
console.info(s); //show C
console.info(o); //show C
答案 2 :(得分:1)
假设我们的网址为https://example.com/?product=shirt&color=blue&newuser&size=m,我们可以使用window.location.search来获取查询字符串:
const queryString = window.location.search;
console.log(queryString);
// ?product=shirt&color=blue&newuser&size=m
然后我们可以使用URLSearchParams解析查询字符串的参数:
const urlParams = new URLSearchParams(queryString);
然后我们在结果上调用其任何方法。
例如,URLSearchParams.get()将返回与给定搜索参数关联的第一个值:
const product = urlParams.get('product')
console.log(product);
// shirt
const color = urlParams.get('color')
console.log(color);
// blue
const newUser = urlParams.get('newuser')
console log(newUser);
// empty string
答案 3 :(得分:0)
例如,对于非编码的URL,就像这个简单的:
<强> https://sergiomcfly.com/index.php?myfirstname=sergio&mylastname=mcfly 强>
将作业打包在一个JS系列中......
<section id="gridSec">
<div class="gridSecWrap">
<div class="gridSecCont">
<div class="gridSecBlock left">
<img src="https://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1513398884/cacimba-do-padre-beach-brazil-365BEACHES1217B.jpg?itok=DUFLlpiW">
</div><div class="gridSecBlock right gridText">
<div class="total-center gridSecBlockWrap">
<p class="dG gridSecText">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
</p>
</div>
</div>
</div>
<div class="gridSecCont">
<div class="gridSecBlock right">
<img src="https://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1513398884/cacimba-do-padre-beach-brazil-365BEACHES1217B.jpg?itok=DUFLlpiW">
</div><div class="gridSecBlock left gridText">
<div class="total-center gridSecBlockWrap">
<p class="dG gridSecText">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
</p>
</div>
</div>
</div>
</div>
</section>
只需在页面的任何位置使用它: - )
urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})