简体   繁体   English

如何仅使用javascript和HTML通过链接传递数据?

[英]How can I pass data through a link using just javascript and HTML?

I have a website where I have a list of names and I need to open up a new page when a name is clicked. 我有一个网站,其中有一个名称列表,单击名称时需要打开一个新页面。 I need this page to display data from the name clicked, so I'm going to need to pass some data through the link. 我需要此页面来显示单击的名称中的数据,因此我需要通过链接传递一些数据。 This could be done with PHP, but can I do it with just HTML and Javascript? 这可以用PHP完成,但是我可以只用HTML和Javascript吗? There has to be a way to handle dynamic content without server-side logic. 必须有一种无需服务器端逻辑即可处理动态内容的方法。

EDIT: I'd like to clarify that the entire purpose of this website is to use an existing website's API in a way that is more streamlined. 编辑:我想澄清一下,该网站的全部目的是以更简化的方式使用现有网站的API。 I just want to tweak their setup and as such writing server logic seems excessive, since I need no persistent data (use the API for that). 我只想调整它们的设置,因此编写服务器逻辑似乎过多,因为我不需要持久性数据(为此使用API​​)。 This question is about how I should pass the API data around the various pages of this custom front end. 这个问题是关于我应该如何在此自定义前端的各个页面之间传递API数据。

You can set uri parameters inline like so: 您可以这样设置内联的uri参数:

<a href="example.com?name=sarah"> Sarah</a>// add a '?' followed by any number of parameters in the format of 'key=value'

You use a question mark to represent the beginning of variables then you can add as many as you would like (with a limit but its pretty high) 您可以使用问号来表示变量的开头,然后可以添加任意数量的变量(有一个限制,但数量非常高)

example.com?key1=value1+key2=value2... example.com?key1=value1+key2=value2 ...

Take a look at this method: 看一下这个方法:

You can pass the data in the query string as: 您可以将查询字符串中的数据传递为:

http://www.yourwebsite.com?name=About http://www.yourwebsite.com?name=关于

You can use this function to get data from the query string: 您可以使用此函数从查询字符串获取数据:

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

The getUrlVars() function will return: getUrlVars()函数将返回:

{
    "Name"    : "About"
}

Now, you can print it anywhere using javascript like: 现在,您可以使用javascript这样的任何地方将其打印:

document.getElementById('p').innerHTML(getUrlVars()["name"]);

or 要么

document.write(getUrlVars()["name"]);

There are a few options, but in general believing that you can change what the server sends down without server side logic is illogical. 有一些选项,但是总的来说,您认为可以更改没有服务器逻辑的服务器发送的消息是不合逻辑的。 What logic would the server use to make changes? 服务器将使用什么逻辑进行更改?

However, if you are serving up the same page but simply changing the names on it, one thing you could do is make the page send down static content without names anywhere and simply put 但是,如果您要提供同一个页面,而只是在页面上更改名称,您可以做的一件事就是使该页面向下发送静态内容,而无需在任何地方添加名称,只需将

<span class='name-of-linked-user'></span>

wherever the name should appear. 名称应出现的任何位置。 At that point, have the link for john doe go to "page.html?name=john%20doe" and add some static jquery to the page. 那时,让john doe的链接转到“ page.html?name = john%20doe”,然后向页面添加一些静态jquery。 From http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html copy the get url parameter code of: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html复制以下代码的get url参数代码:

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];
    }
}
}​

and then a simple jquery loop goes through the static content making it 'dynamic' 然后一个简单的jquery循环遍历静态内容,使其变得“动态”

$('.name-of-linked-user').each(function(){
  $(this).text(GetURLParameter('name'));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过html链接标记传递javascript变量? - How do I pass on javascript variables through a html link tag? 如何在Javascript中传递HTML数据 - How can I pass HTML data in Javascript 如何使用HTML或javascript将变量传递到action属性以进行下载(按钮/链接), - How can i pass a variable into action attribute for download (button/link) using HTML or javascript, 如何使用 javascript 打印孔 HTML 标签。 喜欢如何打印 thi 孔线(<a href="link">文本</a>),而不仅仅是作为链接的文本 - How can I print hole HTML tag using javascript. like how to print thi hole line (<a href="link">text</a>) instead of just text as a link 我如何将html输入数据传递给javascript类 - How can i pass html input data to a javascript class 如何使用 JavaScript 将数据从一个 HTML 页面传递到另一个页面 - How can i pass data from one HTML page to another using JavaScript 如何使用JavaScript将列表组之类的数据从一个HTML页面传递到另一HTML页面? - How can I pass data like list group from one html page to another using JavaScript? 我可以使用javascript隐藏Html链接吗? - Can I hide an Html link using javascript? 如何使用 .htaccess 或 Javascript 使外部链接首先通过附属链接? - How do I make external links pass through an affiliate link first using .htaccess or Javascript? 我如何将html输入数据传递给javascript - How i pass html input data to javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM