简体   繁体   中英

get the href value by clicking on anchor in multiple anchors using jquery

i'm trying to get the href value in multiple links or tag a.and i tried with this code

var val;
$(document).ready(function() {
    $("a").click(function() {
        window.val = $(this).attr("href");
        alert(window.val);
    });

it is working fine for the multiple links and which is inside the file that is local, here few demo links

<a href="sometext1">a</a>
<a href="sometext2">b</a>.....

but problem is i want that href value globally available because i'm using that in other file . My problem is how to make it global, or is there any other way to do it.

and how to write our own function to work the same thing without using $(document).ready function.

this whole thing in one html page but i want only href value in other html page , so if we write our own js function we can use this in both html pages . And that function should return href. but here i dono how to return to $(document).ready function.

You can create an object-based variable:

var screen = {
    link:''
};

And then assign / access on click:

$('a').on('click',function(){
    screen.link = this.href;
    alert(screen.link);
});

I advocate this over assigning variables to the window ... a little more control this way.

Notice I used this.href instead of $(this).attr('href') . As the most interesting man in the world says, I don't always use vanilla JS, but when I do it's about 600,000 times faster.

EDIT So you want to get rid of $(document).ready() huh? Now you're venturing into the shark-infested waters of pure vanilla JS.

var screen = {
    link:'',
    assignLink:function(href){
        screen.link = href;
        alert(href);
    }
},
links = document.getElementsByTagName('a');

if(window.addEventListener){
    for(i = links.length; i--;){
        links[i].addEventListener('click',function(){
            screen.assignLink(this.href);
        });
    }
} else {
    for(i - links.length; i--;){
        links[i].attachEvent('onclick',function(){
            screen.assignLink(this.href);
        });
    }
}

This is just winging it, so don't scathe me if it isn't flawless, its more to make a point. See why jQuery is so handy? All that extra crap is done in the background for you, so that you just need to deal with the burden of $(document).ready() and not have to deal with the rest of this kind of stuff.

EDIT AGAIN So ... you want to access this value across pages?

var screen = {
    link:((localStorage['link'] !== null) ? localSorage['link'] : ''),
    setLink:function(href){
        screen.link = href;
        localStorage['link'] = href;
        alert(href);
    },
    getLink:function(){
        return screen.link;
    }
};

$('a').on('click',function(){
    screen.setLink(this.href);
});

This use of localStorage is just an example ... you can get more elaborate or use cookies if you want IE7- to work, but this just providing ideas. You can set the value whenever you want using the screen.setLink function passing the href , or you can get the value whenever you want using the screen.getLink function.

Declare val outside to make it global and you can use the val inside the function to set the href globally

var val;
$(document).ready(function() {
    $("a").click(function(e) {
        e.preventDefault();
        val = $(this).attr("href");
        alert(val);
    });
});

jsfiddle

Take a look at this example:

<!DOCTYPE html>
<html>
<head>
<title>Try jQuery 1.9.1 Online</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
var val;
$(document).ready(function() {
    $("a").on('click', function() {
        window.val = $(this).attr("href");
        alert(window.val);
        return false;
    });

    $("div").on('click', function() {
       alert (val);        
    });
});

</script>
</head>
<body>
<a href="sometext1">a</a>
<a href="sometext2">b</a>
<div>click here</div>
</body>
</html>

Once you click either the link a or b val will be set. Clicking the div tag will alert you the current reference of val .

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