0

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

3
  • where did you provide Id link in your markup? Commented Apr 14, 2013 at 15:14
  • the a element contain in a DIV defined as link Commented Apr 14, 2013 at 15:15
  • Consider marking a post as "Answer" if you consider it as your solution. Commented Apr 14, 2013 at 15:48

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

9 Comments

thank your plalx! and how can i pass the link to another page using cookies?
@user2279850, Not sure I understand or if you are going in the right direction with cookies. Could you explain why you want to do this?
i want to pass the href path to another page that contain a iframe , and the iframe contain a js file
@user2279850, do you wish to navigate to that other page at the same time? If that's the case you could simply pass the href as a url parameter.
yes, i need to navigate to other page in fact, i want to pass the href as a var to the iframe JS file which in other page i want to navigate
|
0

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);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.