Link to home
Start Free TrialLog in
Avatar of garymg45
garymg45

asked on

<A HREF="#" onClick="window.open ('sitename.html') ...>

I have been studying Javascript and see that open a new window, one option for code appears to be:

IN HEAD

<SCRIPT LANGUAGE="javascript" TYPE ="text/javascript">
<!-- Hide script

function newWindow() {
littleWindow = window.open ('http://www.yahoo.com/', 'littleWin', 'width=300,height=300')
}
// End hiding script -->
</SCRIPT>


IN BODY

<A HREF="javascript:newWindow()">Go To Yahoo</A>


MY QUESTION IS :  
=============

I have also seen it written simply as (using just one line of code):

<A HREF="#" onClick="window.open ('http://www.yahoo.com/', 'littleWin', 'width=300, height=300')">Go To Yahoo</A>



I would like to know the answer to the following questions:

1)  What 'specifically' does the  ' # '  do (that appears between the two quotation marks) following the A HREF???  (HASH)

2)  Is this considered proper code ... to use the  '  #  '  as shown above??

3)  I have several Javascript books and can't seem to find a reference to the  A HREF="#"   (A HREF with the HASH between the quotes) described anywhere.  Please explain why it is used by some javascript programmers, what does it do ... specifically and is it a recommended way of handling the process of opening a new window.

4)  Is there a name or description that identifies the use of the A HREF with the  #  positioned in between the quotation marks??  Please give me as much information on this as possible.

Thanks,
Gary
Avatar of xabi
xabi

Hi there:

The hash "#" is used in link where you don't want to point anywhere, just use the link to call a function like Window.open

Normaly when you create a link with <a href="foo.htm">click here</a> when you click on "click here" you go to the url asigned in href="". But sometimes you don't want to go anywhere, just call a function to open a window, make any calculations, etc. This is the case where you must change this to:

<a href="#" onlick="call_function()">Click Here</a>

What # means? well, as you know # is used in anchor references just to move in the currente page, but without any reference you stay in the same position you are, is just a trick.

xabi
Avatar of knightEknight
Best if you return false in the onClick event, that way the # (or anything else you put in the href) will do nothing:

<A href="#" onClick='myfunction();return(false);'>Click Me</a>
Avatar of garymg45

ASKER

Well, my question still remains ...

WHICH IS THE PROPER WAY TO DO IT??

1)

<A href="#" onClick='myfunction();return(false);'>Click Me</a>


or  


2)

IN HEAD:

<SCRIPT LANGUAGE="javascript" TYPE ="text/javascript">
<!-- Hide script
     function newWindow() {
           littleWindow = window.open ('http://www.yahoo.com/', 'littleWin', 'width=300,height=300')
     }

// End hiding script -->
 </SCRIPT>


IN BODY:

<A HREF="javascript:newWindow()">Go To Yahoo</A>


.............  or

should I also include    return(false);
in the second option shown above?

I want to write it correctly ... regardless of which requires more scripting.  It appears that the longer script might be better, but I want the opinion of someone who has been writing Javascript a lot longer than I.

Plus ... can you tell me a little more specifically (really specific) as to what is happening when you use the   #  (HASH)  in the quotes?  If it is a trick ... what is the trick doing ... in actuality!

And, is this 'trick' of using the HASH in the quotes, something that I can look up in a book?  Because I don't see it anywhere.

Please let me know your thoughts!

Thanks,
Gary
To KnightEKnight,

Please explain:

<A href="#" onClick='myfunction();return(false);'>Click Me</a>

I am not familiar with     return(false);
and can not find a description of it anywhere.  I have a book called PURE Javascript by SAMS as well as Javascript 3rd Edition/Visual Quickstart Guide  and I can't find   return(false);  indicated anywhere.

Can you explain:

1)  What it is?
2)  How it works?
3)  How did you figure out to use this code??  I'm confused.

Thanks,
Gary
You can return anything from a function or a handler ( true, false, 5, "Hi", obj, etc)  I don't recall where I first learned this, but it should be in any HTML/JavaScript reference -- that anchor tags have an onClick handler, and that returning false in the onClick handler will cancel the href action.  Returning true, however will perform the href action.  The original purpose was so that you can do something like this:

<A href='www.yahoo.com' onClick='return(confirm("Are you sure?"));'>Yahoo</a>

Anyway, I would modify your code to do this instead:


IN HEAD:

<SCRIPT LANGUAGE="javascript" TYPE ="text/javascript">
<!-- Hide script
function newWindow()
{
   var littleWindow = window.open('http://www.yahoo.com/', 'littleWin', 'width=300,height=300');
}
// End hiding script -->
</SCRIPT>

IN BODY:

<A HREF="#" onClick='newWindow();return(false);'>Go To Yahoo</A>

I hope that is helpful, but give some points to xabi too.
_____________________________________________________________________________________________
give me the points dude.
Adjusted points to 150
ASKER CERTIFIED SOLUTION
Avatar of ianB
ianB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Gary and Ian,
I hope the info above was helpful.
YES...  Thank you!!