<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
garymg45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xabiCommented:
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
0
knightEknightCommented:
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>
0
garymg45Author Commented:
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
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

garymg45Author Commented:
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
0
knightEknightCommented:
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.
_____________________________________________________________________________________________
0
mrquijaCommented:
give me the points dude.
0
garymg45Author Commented:
Adjusted points to 150
0
ianBCommented:
Hi,

I am answering this question so the question can be saved as a PAQ.

In http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=10258268 


garymg45 asked for the question can be split :

1)  Provide 50 points to xabi
2)  Proivde 100 points to knightEknight

xabi points are at Q.10258561
and knightEknight is at Q.10258562

Ian
Community Support @Experts Exchange
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
knightEknightCommented:
Thanks Gary and Ian,
I hope the info above was helpful.
0
garymg45Author Commented:
YES...  Thank you!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.