如何通过 url 将参数传递给 javascript 并将其显示在页面上? [英] How to pass a parameter to a javascript through a url and display it on a page?

查看:24
本文介绍了如何通过 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_idGET["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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆