admin管理员组文章数量:1026989
So, here's the main code, which checks the URL of the current page for the string ?a= then sets the variable ee_roomname to anything after that.
<script type="text/javascript">
var ee_roomname = unescape(location.href.substring(location.href.lastIndexOf("?a=")+1))
if(ee_roomname.indexOf("?") != -1) ee_roomname = "";
</script>
But what, if I want to add more variables, maybe after this value. For ex. here's a possible url:
.html?a=hijk
Thus, the variable ee_roomname will be set to hijk
Now if we add some more tags, it will look like this:
.html?a=hijk&b=lmno&c=pqrs
Now ee_roomname will be set to hijk&b=lmno&c=pqrs, which is not what I want.
I want the code to only track the a variable/parameter's value from the URL.
What needs to be changed, and how, in order to make it work?
Maybe provide a code I can use, too. (not too big fan of "do this and this" and such)
So, here's the main code, which checks the URL of the current page for the string ?a= then sets the variable ee_roomname to anything after that.
<script type="text/javascript">
var ee_roomname = unescape(location.href.substring(location.href.lastIndexOf("?a=")+1))
if(ee_roomname.indexOf("?") != -1) ee_roomname = "";
</script>
But what, if I want to add more variables, maybe after this value. For ex. here's a possible url:
http://abc.de/fg.html?a=hijk
Thus, the variable ee_roomname will be set to hijk
Now if we add some more tags, it will look like this:
http://abc.de/fg.html?a=hijk&b=lmno&c=pqrs
Now ee_roomname will be set to hijk&b=lmno&c=pqrs, which is not what I want.
I want the code to only track the a variable/parameter's value from the URL.
What needs to be changed, and how, in order to make it work?
Maybe provide a code I can use, too. (not too big fan of "do this and this" and such)
- Looks like you want something like this – Gaʀʀʏ Commented Jun 21, 2012 at 16:40
- @le_garry Exactly what I was looking for. Could you post that as an answer so I can accept it? – SeinopSys Commented Jun 21, 2012 at 16:44
3 Answers
Reset to default 5Here is a function to retrieve url parameters
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
How to retrieve the url parameters in your javascript
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
View the Source
For your bonus: if you split the string at the '?' and take the first half, that will give you the "pure" absolute url.
to answer the first question, you could also try:
<script>
var query = window.location.search.substring(1);
var getv = query.split("&");
var pair = new Array();
for (var i=0; i < getv.length; i++) {
pair[i] = getv[i].split("=");
}
</script>
pair is an array of all get variables and values like: [["h", "1"], ["g", "2"]] if ?h=1&g=2
hope that helps.
Try to parse the entire url using this URL Parsing library in JavaScript.
So, here's the main code, which checks the URL of the current page for the string ?a= then sets the variable ee_roomname to anything after that.
<script type="text/javascript">
var ee_roomname = unescape(location.href.substring(location.href.lastIndexOf("?a=")+1))
if(ee_roomname.indexOf("?") != -1) ee_roomname = "";
</script>
But what, if I want to add more variables, maybe after this value. For ex. here's a possible url:
.html?a=hijk
Thus, the variable ee_roomname will be set to hijk
Now if we add some more tags, it will look like this:
.html?a=hijk&b=lmno&c=pqrs
Now ee_roomname will be set to hijk&b=lmno&c=pqrs, which is not what I want.
I want the code to only track the a variable/parameter's value from the URL.
What needs to be changed, and how, in order to make it work?
Maybe provide a code I can use, too. (not too big fan of "do this and this" and such)
So, here's the main code, which checks the URL of the current page for the string ?a= then sets the variable ee_roomname to anything after that.
<script type="text/javascript">
var ee_roomname = unescape(location.href.substring(location.href.lastIndexOf("?a=")+1))
if(ee_roomname.indexOf("?") != -1) ee_roomname = "";
</script>
But what, if I want to add more variables, maybe after this value. For ex. here's a possible url:
http://abc.de/fg.html?a=hijk
Thus, the variable ee_roomname will be set to hijk
Now if we add some more tags, it will look like this:
http://abc.de/fg.html?a=hijk&b=lmno&c=pqrs
Now ee_roomname will be set to hijk&b=lmno&c=pqrs, which is not what I want.
I want the code to only track the a variable/parameter's value from the URL.
What needs to be changed, and how, in order to make it work?
Maybe provide a code I can use, too. (not too big fan of "do this and this" and such)
- Looks like you want something like this – Gaʀʀʏ Commented Jun 21, 2012 at 16:40
- @le_garry Exactly what I was looking for. Could you post that as an answer so I can accept it? – SeinopSys Commented Jun 21, 2012 at 16:44
3 Answers
Reset to default 5Here is a function to retrieve url parameters
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
How to retrieve the url parameters in your javascript
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
View the Source
For your bonus: if you split the string at the '?' and take the first half, that will give you the "pure" absolute url.
to answer the first question, you could also try:
<script>
var query = window.location.search.substring(1);
var getv = query.split("&");
var pair = new Array();
for (var i=0; i < getv.length; i++) {
pair[i] = getv[i].split("=");
}
</script>
pair is an array of all get variables and values like: [["h", "1"], ["g", "2"]] if ?h=1&g=2
hope that helps.
Try to parse the entire url using this URL Parsing library in JavaScript.
本文标签: phpGetting parameters from page urlStack Overflow
版权声明:本文标题:php - Getting parameters from page url - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610365a2158967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论