Reputation: 25048
I have a script like:
<script type="text/javascript">
$("a.link").on("click",function(){
window.open('http://site.com/fir.php','_self',false);
});
</script>
and I am using it as
<a href="fir.php" class="link">
fir
</a>
How can I make only one script and pass as variable the url I want to open?
Upvotes: 1
Views: 50
Reputation: 28763
Try like this
<script type="text/javascript">
$("a.link").on("click",function(){
window.open('http://site.com/'+$(this).attr('href'),'_self',false);
});
</script>
You can use .prop also like
window.open('http://site.com/'+$(this).prop('href'),'_self',false);
Upvotes: 2
Reputation: 265
You can directly use this HTML code.May be it will helps you
$(function(){
$("a.link").on("click",function(e){
window.open(this.href, '_self', false);
});
});
Upvotes: 1
Reputation: 74738
You can use this.href
to get the specific link's href to navigate to open in window but make sure to stop the link's default behavior with .preventDefault()
:
<script type="text/javascript">
$("a.link").on("click",function(e){
e.preventDefault();// or return false;
window.open('http://site.com/'+this.href, '_self', false);
});
</script>
Upvotes: 3