www.webdeveloper.com
Results 1 to 5 of 5

Thread: <a href="" onClick="..."> problem

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    <a href="" onClick="..."> problem

    Hi, very nice to JS and totally new to this forum, so please keep it simple if you can.

    I've read that you can make a link with nothing in the URL inorder to use the onClick event handler. Essentially, what I'd like to do is open a new window with my specifications, rather than directly link to the page. What I've tried is this:

    HTML Code:
    <a href="" onClick="OpenNewWindow()">Click here to open a new window</a>
    Where OpenNewWindow() is the function that does the work. The new window opens just fine, so I'm not concerned that much about the function, as about the statement I posted.

    I find that whenver I do something like this, the browser (IE, generally) takes the <a href=""> to be a link to the home directory. For example, if I'm working locally, when I click on the link the new window will open, but the main window will jump to the index of the directory. Similiarly, if I'm working on my site, the main window will jump to /index.html. This is all very annoying and I haven't found a solution to it.

    I'm not sure whether the solution would be on the HTML or the javascript end of things, but if you could help me out, I'd appreciate it greatly.

    Thanks!

  2. #2
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    342
    <a href="#" onClick="window.open();">Click here to open a new window</a>

  3. #3
    Join Date
    Jun 2005
    Posts
    4
    Thanks a lot, it worked like a charm. I suppose I should assign a tag to that spot on the page, though, because it jumps to the top, which is not what I'd like to happen.

    So it turns out the answer was an html answer rather than one involving the javascript. Oh well. Thanks anyway!

  4. #4
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    <a href="#" onClick="window.open();return false;">Click here to open a new window</a>

    to make sure that the current page isn't reloaded when the new window opens.
    Stephen

  5. #5
    Join Date
    Jun 2005
    Posts
    4
    I didn't see that at first, but thanks for that little "return false" hint! I was dealing with problem by creating a <a name="...."> to get the referring page to at least be in the correct place after the link was clicked. This fixes that issue. Great!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles