PDA

View Full Version : How to Open links in New Tab of browser with Javascript or VBscript



Raj
04-18-2007, 01:59 PM
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?

jscheuer1
04-18-2007, 02:08 PM
That behavior is configured by the user and, as far as I know, only by the user.

Raj
04-18-2007, 02:16 PM
Thanks John..... but there has to be a way with some kind of script....

jscheuer1
04-18-2007, 02:21 PM
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.

Raj
04-18-2007, 02:28 PM
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 ?

jscheuer1
04-18-2007, 04:40 PM
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.

jscheuer1
04-18-2007, 04:48 PM
I may be over thinking this. Just use:


<a href="some.htm" target="_blank">Link Text</a>

That's, I believe, the best you can do.

jscheuer1
04-18-2007, 05:25 PM
OK, here's another idea, get the user to do it:


<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>

Raj
04-19-2007, 08:08 AM
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?

jscheuer1
04-19-2007, 03:46 PM
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.

boxxertrumps
04-19-2007, 03:52 PM
The standard key for opening in a new tab is middle click...
Works in FF, IE7, and Opera.
The user can configure/force all the links to open in new tabs. it's their decision where they want a new page to open.

jscheuer1
04-19-2007, 04:07 PM
The standard key for opening in a new tab is middle click...
Works in FF, IE7, and Opera.
The user can configure/force all the links to open in new tabs. it's their decision where they want a new page to open.

We knew that. Raj is determined to take that decision out of the user's hands. I don't think it can be done and have already stated as much in this thread.

Additionally, I just tested the event property keyCode. It also is a getter and not a setter so, as I also said earlier:


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

But we cannot.

Raj
04-20-2007, 08:01 AM
Is there any way of controlling the DLL ? I am also looking for an option through ActiveX. Since we can local hard drive with ActiveX and Javascript there might be way to control the browser? Don't you think so?

jscheuer1
04-20-2007, 08:08 AM
Is there any way of controlling the DLL ? I am also looking for an option through ActiveX. Since we can local hard drive with ActiveX and Javascript there might be way to control the browser? Don't you think so?

I had mentioned earlier that both Active X and Java (not javascript) might be able to do this but, all users in their right mind that know anything about these languages would block anything of the sort. And, this level of client manipulation is blocked by default in all current release browsers that I know of.

I wish to repeat, "Why not just let the user decide?" You never answered that question.

Raj
04-20-2007, 08:26 AM
No John, this is a requirement of my company that is why I am looking for this solution. For non-tabbed browsers a new window should open and for tabbed browsers it should open in a new tab.

jscheuer1
04-20-2007, 08:42 AM
a requirement of my company

That doesn't tell me much and doesn't really answer the question. What's your company? Is it just you? Why is it a requirement?

If I had a client who wanted this, I would just say; "Can't and shouldn't be done."

The target="_new" attribute of a link will open it in a new window or tab depending upon the browser:


<a href="some.htm" target="_new">Link Text</a>

If you want to avoid the target attribute due to validation concerns, use javascript:


<a href="some.htm" onclick="window.open(this.href);return false;">Link Text</a>

That's the best you can do.

Raj
04-20-2007, 09:01 AM
Well John, I think you understand that every project has some requirements. I wouldn't be able to give you the details but one such requirement (if possible) is this. Opening some links of a html page in new window or Tab, again if possible.

Anyways, currently I feel it will be hard to open in a new Tab, may be in future.

Thanks Alot for your help & time. Really appreciate it...

tech_support
04-20-2007, 09:48 AM
You can't. Period.

techno_race
04-20-2007, 10:58 PM
In IE on Windows, to open a link to main.htm that is an image called home.jpg that says Home by pressing Alt+H, use the code:

<a href="main.htm" accesskey="h"><img border="0" src="home.jpg" alt="Home"></a>

tonywe
04-26-2007, 02:57 PM
Just go to: http://browservulsel.blogspot.com/2006/03/single-left-click-tab-opener-user.html

this guy seems to have cracked it............I hate it when people say something cant be done, its so anoying.

jscheuer1
04-26-2007, 04:11 PM
Just go to: http://browservulsel.blogspot.com/2006/03/single-left-click-tab-opener-user.html

this guy seems to have cracked it............I hate it when people say something cant be done, its so anoying.

That appears to be a customization for the greesemonkey FF browser extension. We already knew that users could customize their own browsers to behave as desired. How (if at all) does that work on a live web page for all FF and IE 7 users? If we can get everybody to switch to FF and install GM, it might just work out.

boxxertrumps
04-26-2007, 11:43 PM
greasemonkey is just an extention that appends your own JS docs to the head section, they work like any normal script.

Unless I've been misled.

*looks slowly around the room, backs out of the door*

jscheuer1
04-27-2007, 12:08 AM
greasemonkey is just an extention that appends your own JS docs to the head section, they work like any normal script.

Unless I've been misled.

*looks slowly around the room, backs out of the door*

Not so in this case. The script uses:


GM_openInTab(a.href);

to open in a tab. That looks like a GM proprietary function to me as, 'GM_openInTab' occurs nowhere else in the code.

If you can put up a demo page and show me how it works in FF and IE 7, then I will be convinced.


I hate it when people say something cant be done, its so anoying.

I don't mind it so much as solutions that don't work.

mburt
04-27-2007, 11:11 AM
I hate it when people say something cant be done, its so anoying.
It didn't work either. It kept pointing me in the same page.

Raj
10-25-2007, 06:22 AM
So guys are we saying that we don't have any answer to this???

prantoor
03-06-2008, 11:58 AM
What you want can u write me details.do u want to open a page in new tab by javascript.
if yes then i can help you for Firefox but i have no idea on IE. i am searching for IE.

Matey
08-22-2008, 02:32 PM
I may be over thinking this. Just use:


<a href="some.htm" target="_blank">Link Text</a>

That's, I believe, the best you can do.

(I know it is an old thread but..)

Thanks a lot. I have been looking on the web for 2 days but found nothing that worked! This is the only code that really works! and works great!

Thanks a lot!

jscheuer1
08-22-2008, 04:42 PM
I may be over thinking this. Just use:


<a href="some.htm" target="_blank">Link Text</a>

That's, I believe, the best you can do.


(I know it is an old thread but..)

Thanks a lot. I have been looking on the web for 2 days but found nothing that worked! This is the only code that really works! and works great!

Thanks a lot!

No problem. Now, the original question was wanting to open in a new window, except if the browser has tabbed browsing, then in that case open a new tab. Most browsers will just behave that way if you ask for the new window in the most generic terms, either as quoted above or, using javascript:


<a href="whatever.htn" onclick="window.open(this.href);return false;">Link Text</a>

What you cannot do is force a new window or tab if the browser is using other than its default settings for whatever it is that you want. You can force a new window for all in some cases (for users with default settings), but almost never (the exception being if the user has some add on that can be tricked into thinking your script is really the user's own self configuration script) force a new tab.

Just to be clear, whatever you try, if the user has their browser configured to maintain control of such things, the user will win the 'battle'.

ekotaktik
10-09-2008, 05:17 AM
Could it possibly be as simple as this?


form = document.createElement("form");
form.method = "GET";
form.action = "http://www.your.link.url.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();

jscheuer1
10-09-2008, 06:13 AM
Could it possibly be as simple as this?


form = document.createElement("form");
form.method = "GET";
form.action = "http://www.your.link.url.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();

No. For one, that's not simple. For another, all of the caveats that I mentioned in my previous post will still apply.

Also note that you have an undeclared variable in the global scope (form). Oh, and it's not HTML code.

However, none of that's to say it won't work in some cases. Without testing I cannot say for sure, but it looks workable. Recommended? Definitely not.

anandpvijayan
11-03-2008, 09:17 AM
<a href=”newwindow.html” target=”_blank”>New Window</A>

This is all you need to do. Works for me.

tech_support
11-03-2008, 09:43 AM
Already answered....