简体   繁体   中英

get href path from <a> attribute and pass to next html page using JavaScript

I have two links:

<div class="holder_content1" id="link" >   
<p> <a href="PDFJS/pdfFile/Tut02_v2-Ans.pdf"  onclick="getPDFpath()">Tut02_v2-Ans</a>        
<p><a href="PDFJS/pdfFile/DA_Chap02_v3.pdf"  onClick="getPDFpath()">DA_Chap02_v3</a>

I need to get link from anchor attribute using Javascript. Here is my javascript function.

function getPDFpath(){
    var path = document.getElementById('link');
    var s = path.getAttribute('a');
    alert(s.href);  
}

Need link after pressing click. Thank you

Pass the link reference to the getPDFpath function.

<a href="PDFJS/pdfFile/DA_Chap02_v3.pdf"  onClick="getPDFpath(this)">DA_Chap02_v3</a>

You can now access the href property of the element.

function getPDFpath(link){
    //navigate to another page by passing the href as the `pdflink` query param.
    location.href = 'my_other_page.html?pdflink=' + encodeURIComponent(link.href);
}

However, you don't have to use any javascript code to do this:

<a href="my_other_page.html?pdflink=PDFJS/pdfFile/DA_Chap02_v3.pdf">DA_Chap02_v3</a>

Here are functions to set and get cookies.

function setCookie(key, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    value = escape(value) + ((exdays === undefined) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = key + "=" + value + '; path=/';
}

function getCookie(key) {
    var i, x, y,
        ARRcookies = document.cookie.split(";");

    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g,"");
        if (x == key) {
            return unescape(y);
        }
    }
}

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.

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