如何通过url将参数传递给javascript并将其显示在页面上? [英] How to pass a parameter to a javascript through a url and display it on a page?
本文介绍了如何通过url将参数传递给javascript并将其显示在页面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个这样的网址:www.mysite.com/my_app.html
For instance, I have a url like this : www.mysite.com/my_app.html
如何将值Use_Id = abc传递给它并使用javascript在该页面上显示?
How do I pass a value " Use_Id = abc " to it and use javascript to display on that page ?
推荐答案
如果不需要外部编写自己应该不会太难图书馆。
Shouldn't be too difficult to write your own without the need for an external library.
// www.mysite.com/my_app.html?Use_Id=abc
var GET = {};
var query = window.location.search.substring(1).split("&");
for (var i = 0, max = query.length; i < max; i++)
{
if (query[i] === "") // check for trailing & with no param
continue;
var param = query[i].split("=");
GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || "");
}
用法: GET.Use_id
或获取[Use_id]
。您还可以使用GET 中的Use_id(将返回true或false)检查参数是否存在,即使它具有空值。
Usage: GET.Use_id
or GET["Use_id"]
. You can also check if a parameter is present even if it has a null value using "Use_id" in GET
(will return true or false).
这篇关于如何通过url将参数传递给javascript并将其显示在页面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文