Forum Moderators: open

Message Too Old, No Replies

Onclick to open a new window

         

akula

4:17 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



I have created a button that when clicked will popup an alert box and then send the user to a new website. Problem is, I want to open the external site in a new window like target="_blank" but I can't get it to do that; it opens the site in the current window.

<input name="button" type="button" class="extsite" target="_blank" onClick="parent.location='http://www.externalsite.com'; alert('We are sending you to an external site')" Value="Click to visit the site.">

I tried to make it a regular anchor link but then the Google Toolbar kept blocking the new window as a popup.

I've searched for a solution with no luck. My only thought is I have to create some kind of javascript function to handle this?

Thanks.

penders

11:09 pm on Feb 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The 'target' attribute is not valid on an <input> element. The target attribute on an anchor (<a>) indicates the target of the href attribute.

You should be able to do what you are after with a regular anchor....

<a href="http://www.externalsite.com" onclick="alert('We are sending you to an external site');window.open('http://www.externalsite.com','external');return false;" title="Opens the external website in a new window">Click to visit the site</a>

This will at least enable your users to get to the external site if they have JS disabled (albeit in the same window). If the Google Toolbar is blocking this, then I recon it's going to block every popup - although you can try getting rid of the alert('We are...'); ...?

The window.open() function can take a 3rd parameter which controls the size and gadgets that appear on the popup window, by default it will be just another window with everything.

Popup blockers should not (by default) block popups that are a result of a direct user action, such as clicking on a link.

Using a JS function will not help you in this respect, other than making your code a lot more tidy.

akula

4:09 am on Feb 22, 2007 (gmt 0)

10+ Year Member



Thanks for the reply and help. I wanted it to be in a button form but changed it to a simple link instead and still have the alert and opening in a new window which was more important.

<a href="http://www.externalsite.com" target="_blank" class="ext" onclick="alert('External site')";>CLICK TO Visit External site</a>

carguy84

6:37 am on Feb 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onclicke="alert('Whaever'); window.open('url', 'newWindow');"
is what you are looking for.

akula

6:53 am on Feb 22, 2007 (gmt 0)

10+ Year Member



Well, I spoke to soon about the code working. It works in firefox but IE7 still blocks with Googlebar.

I got this to work though:

<a class="activate" target="_blank" href="javascript:alert('Alert text');location.href='http://www.externalsite.com'">Click</a>

Its doesn't work if Javascript is disabled though.

The code you mentioned still gets blocked by the toolbar but thanks anyway.

rocknbil

9:18 am on Feb 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



haha . . . well your alert isn't going to work if Javascript is disabled anyway. :-)

<form style="margin:0;padding:0;" method="get" action="http://www.example.com" target="new window"><input type="submit" onClick="alert('thanks for visiting!');" value="I'm outta here"></form>

Didn't test against popup blockers, but shouldn't give you grief.

akula

2:56 pm on Feb 22, 2007 (gmt 0)

10+ Year Member



Still gets blocked by Google toolbar.

penders

7:15 pm on Feb 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



akula: It looks as if you've found a way to get around Googlebar's popup blocker (under IE7) ...? But I'm a bit surprised that some of the methods suggested here, fail (as if the 'blocker' setting is set particularly high), and yours does not get blocked?! Almost suggesting the popup blocker itself is at fault?!

Personally I only use the default popup blockers (on their default settings) that come as part of FF and IE and have no problem with displaying correctly coded popups, and nor am I overrun with annoying popups.

I'm curious, does the following get blocked by your 'Googlebar popup blocker' under IE7...?

<a href="http://www.example.com" onclick="window.open('http://www.example.com','popup');return false;">Click here to popup</a>

akula

7:46 am on Feb 26, 2007 (gmt 0)

10+ Year Member



No, that does not get blocked by the toolbar. It must be the popup alert box that is triggering the blocker.

akula

8:13 am on Feb 26, 2007 (gmt 0)

10+ Year Member



Now I added this
<script language="JavaScript">
function LinkAlert ()
{
alert ("You are about to leave this site");
}
</script>

and added LinkAlert to it:

<a href="http://www.example.com" onclick="window.open('http://www.example.com','popup');LinkAlert();return false;" >Click here to popup</a>

And that works,but IF I put the LinkAlert before the window.open, Google blocks it.

penders

8:40 am on Feb 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...but IF I put the LinkAlert before the window.open, Google blocks it.

It looks like the Googlebar is perceiving that the window.open() command is not as a result of a direct user action, with another command (that requires user action) occuring before it - which in a way is correct. Although that doesn't really explain why your method above works OK...?

<a target="_blank" href="javascript:alert('Alert text');location.href='http://www.externalsite.com'">Click</a>

Interesting... thanks for testing! :)