Does anybody know a javascript code that will detect the dynamic url of the page (?q=Chicken) and setup a javascript variable called 'query'. I would then like to display the variable as text.
This will all be onload. Can this also be done with a textbox? (Input type text - set text to query)
I cannot use php of asp. Just html and javascript!
Take a look at this question: Get current URL in web browser
Once you have the full URL, you should be able to parse it using regexs or other any other method that you can think of. Then, you could just set the text of an html element to that query. Or you could create a whole new element and append it somewhere
您可以使用
document.location.search
Regex can get pretty messy. Check out this Stackoverflow answer instead:
How can I get query string values in JavaScript?
(it links to http://css-tricks.com/snippets/javascript/get-url-variables/ which suggests using this snippet of code:)
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
Usage:
Given this url: http://www.example.com/index.php?id=1&image=awesome.jpg
Calling getQueryVariable("id")
- would return "1". Calling getQueryVariable("image")
- would return "awesome.jpg".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.