Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: How to Open links in New Tab of browser with Javascript or VBscript

  1. #1
    Join Date
    Apr 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry How to Open links in New Tab of browser with Javascript or VBscript

    I have tried a lot but couldnot find any script which can help me open my links from an html page in a NEW TAB of browsers (IE7 or Firefox).

    Can some one help me?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That behavior is configured by the user and, as far as I know, only by the user.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks John..... but there has to be a way with some kind of script....

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not really, many things about the browsing experience are solely under the control of the user. Javascript and other coding languages can enhance the browsing experience but, they cannot take over the browser except in the cases of Java and Active X. These coding languages allow the programmer to take control of the user's computer but, there are safeguards to prevent this and so, they often do not achieve their objectives.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is there any way through which we can trigger keyboard short cut with a click of a mouse. Eg: activate "ctrl+t" with a click on a link ?
    Last edited by Raj; 04-18-2007 at 01:46 PM.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Perhaps, but I really don't think so. You used to be able to create a new event object:

    var ev=new Event()

    and assign its type and other property values, we would use (if we could still do this):

    ev.type=keydown
    ev.keyCode=17

    Then we could fire it with an onclick event of a link but, even if this were still allowed, I have no idea if it would work.

    The reason this whole thing is so tricky is that as far as javascript is concerned, opening a link in a new tab is the same thing as opening one in a new window. It just depends upon how the user has their browser configured.

    That and the fact that javascript doesn't really create anything that is usually user initiated, it just reacts to things that the user does and/or manipulates styles, content, and layout.

    Finally, what would be so bad about just opening a new window? For users that have their browsers configured to open new windows in tabs, they will get a tab, others will get a new browser instance. What could be so important about your content that it just has to be in a tab? Also, even if this could be done, some browsers surly could override it and tab-less browsers could not comply.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I may be over thinking this. Just use:

    HTML Code:
    <a href="some.htm" target="_blank">Link Text</a>
    That's, I believe, the best you can do.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, here's another idea, get the user to do it:

    HTML Code:
    <a href="some.htm" onclick="if(!event.ctrlKey&&!window.opera){alert('Please Try Again while holding down the Ctrl Key');return false;}else{return true;}" target="_blank">Link Text</a>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Apr 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John, I tried your idea of handling the even. I was able to alert the values but could not Fire an event.

    Here is the code:

    <html>
    <head>
    <script type="text/javascript">
    function foo(elem, evt){
    /*alert(
    [
    "elem : " + elem.nodeName,
    "shiftKey : " + evt.shiftKey,
    "keyCode : " + evt.keyCode
    ].join("\n")
    );
    */

    evt.ctrlKey=true;
    evt.keyCode=17;
    event.srcElement.fireEvent("onclick",evt);

    }
    </script>

    </head>
    <body>
    <A href="#" onclick="foo(this, event)" onkeypress="foo(this, event)">test</A>
    </body>
    </html>

    Any idea what I doing wrong?

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The ctrlKey property is only a getter so, no matter what else may or may not be wrong with your code, you cannot set the ctrlKey property of an event as it appears that you are trying to do. I'm not certain but the keyCode property may also be only a getter. If it isn't, and is also a setter, it still may not be able to influence what the browser does as, event results set by javascript have no particular claim on controlling browser behavior although, they often do have that effect.

    Using:

    event.srcElement

    is for IE only.

    There could also be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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