简体   繁体   English

使用HTML和JavaScript将href值传递到另一个页面

[英]Passing href value to another page using html and javascript

在此处输入图片说明

As mentioned above, i need to create a class="donate" that will be used in anchor tag which will redirect and pass the value of href to my another page ie -mysite.com/donate.html, 如上所述,我需要创建一个将在锚标记中使用的class="donate" ,它将重定向并将href的值传递到另一个页面,即-mysite.com/donate.html,

where donate.html will be able to grab that href value . donate.html在哪里可以获取该href值。 so that when user click on " continue to download" will be able to download the file . 这样,当用户单击“继续下载”时,便可以下载文件。

Please let me know whether is it possible or not . 请让我知道是否可能。 I would prefer HTML and javascript only . 我只喜欢HTML和javascript。 as I don't host webpages i use blogger . 因为我没有托管网页,所以我使用Blogger。

I've seen similar concept in templateism.com, something like: 我在templateism.com中看到了类似的概念,例如:

www.templateism.com/p/preview.html?=www.templateism.com/10/2010/anypost.html www.templateism.com/p/preview.html?=www.templateism.com/10/2010/anypost.html

you can use the link like: 您可以使用以下链接:

mysite.com/donate.html?www.yoursite.com.otherpage.html

and then using javascript: 然后使用javascript:

var href = document.location.href;
var link = href.split('?')[1];

Basically you need to pass the value you required in querystring like mysite.com/donate.htm?key=value. 基本上,您需要在查询字符串中传递所需的值,例如mysite.com/donate.htm?key=value。

On donate.htm page you can use this function mentioned here How can I get query string values in JavaScript? 在donate.htm页面上,您可以使用此处提到的此函数。 如何获取JavaScript中的查询字符串值? to fetch the value you supplied in querystring by passing the key. 通过传递键来获取您在querystring中提供的值。

You can use the JavaScript (jQuery required) code below to attach a click event handler to all links with the donate class. 您可以使用下面的JavaScript代码(必需的jQuery)将click事件处理程序附加到donate类的所有链接上。

JS on page1.html and page2.html page1.html和page2.html上的JS

$(document).ready(function() {
    $('a.donate').click(function() {
        // Retrieve the href value and store it in a variable.
        var href = $(this).attr('href');

        // Redirect a user to the desired page and passes the href value in query string.
        window.location.href = 'http://example.com/donate.html?href=' + href;

        // Ensure the original click event handler is not triggered.
        return false;
    })
});

After clicking a link the user will be redirected to the donate page where you can parse the query string and obtain the value of the href (which is what was in the <a href="..."> on the previous page). 单击链接后,用户将被重定向到捐赠页面,您可以在其中解析查询字符串并获取href的值(这是前一页<a href="...">中的内容)。

There are many ways to parse the query string but the following quick&dirty code should work for you. 有很多解析查询字符串的方法,但是以下快速和肮脏的代码应该适合您。

JS on donate.html JS on donate.html

var location = document.location.href;
var queryString = location.split('?')[1];

// In the href variable is now what you are looking for.
var href = queryString.substr(5);

Here try this For example...... This is your html code 在这里试试这个例如……这是您的html代码

<a href="http://example.com/donate.html?www.yoursite.com.otherpage.html"></a>

As Ashkan said, use his javascript to parse the previous url from it... Paste this code on donate.html 正如Ashkan所说,请使用他的JavaScript解析其中的先前网址...将此代码粘贴到donate.html上

<script>
var href = document.location.href;
var link = href.split('?')[1];
window.location.href = "http://"+link;// This will show you the link
</script>

Hope this helps; 希望这可以帮助;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM