0

So ive got a JS function that's run when multiple html href's are clicked. That is to say, i have about 4 different href's that look similar to this:

<a href='#' onClick="myPopup()">New Window</a>

and my JS looks like this :

<script language="javascript" type="text/javascript"> 
<!--
function myPopup() 
{
    window.open( "account_edit.php", "myWindow", "status = 1, height = 300, width = 300, resizable = 0" )   
}
//-->
</script>

my point is, i dont want to have to re-write that one block of script another 3 times. Is there a way i can get the script to tell which href was clicked, then open up its respective page? Obviously normal href's are out of the picture, else i would do it. Thanks for the help!

1
  • This doesn't help if you aren't using jQuery, but if you were: You can use [jQuery.data]([http://api.jquery.com/jQuery.data/) method to extract an arbitrary value from your anchor tag. Commented Sep 27, 2013 at 15:59

3 Answers 3

4
function myPopup(target) 
{
    window.open( target, "myWindow", "status = 1, height = 300, width = 300, resizable = 0" )   

then call it from each <a> with a different target:

<a href='#' onClick="myPopup('account_edit.php')">New Window</a>

Bonus Track: you can make this unobtrusive with jquery (much cleaner!)

HTML:

<a href="account_edit.php">New Window</a>

JS:

$(function(){
    $('a').click(function(e){
        e.preventDefault(); //prevents browser from following link
        window.open( this.href, "myWindow", "status = 1, height = 300, width = 300, resizable = 0" )
    });
});

note: if you don't specify a fqdn, the browser will prepend your current URL path to the href (so if you call this from abc.com/test/test.htm the popup will open in abc.com/test/account_edit.php)

Sign up to request clarification or add additional context in comments.

3 Comments

Oh wow, im surprised i didnt remember that!! I havent wrote java in over a year! :/ Thanks man!!
I think you mean javascript =)
I trued to but it wouldnt let me and i had to wait but now it's okay :)
1

Well, you could pass something unique through the parameter:

<a href='#' onClick="myPopup('link1')">New Window</a>

then here:

<script language="javascript" type="text/javascript"> 
function myPopup(link) 
{
    alert('you cliked on link '+link); //or do whatever your want
}

</script>

Comments

1

just pass some arguments within your javascript function call like New Window, New Window and so on

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.