Link to home
Start Free TrialLog in
Avatar of Ivicarp
Ivicarp

asked on

Why when window.open(url) called directly from input botton bypasses popblockers but when called through another function gets blocked

Why when the javascript window.open(url) function is called directly from an input botton, it  bypasses popblockers but when called through another function it gets blocked?

See the code attached.
Test1 button is via a function that first uses pagemethods to process the string for the address. I need to use a server side function (The pagemethod works perfect, it is not an issue) The fact that is called inderectly somehow alerts the popup blockers.
Test2 button has the url string in it.
Safari, Firefox, Chrome, Opera and IE will open a second window when using button 2 with no problems even with blockers ON.

Only IE will open with button 1, Firefox, Chrome and Opera will advise that the action was blocked and allow override. Safari will provide no clue that the call is been blocked.

I need for button 1 to behave like button 2.
Any help will be appreciated. Please any solution if posible in vb.
Thank you.



<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="jsserverfnvb._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager" runat="server"
       EnablePageMethods="true"></asp:ScriptManager>
<script language="javascript" type="text/javascript">
    function MyMethod_Result(ResultString) {
        var urlmap = 'http:\/\/www.bing.com/maps/?v=' + ResultString;
        window.open(urlmap);
    }

    function CallMyMethod() {
        PageMethods.MyMethod("World", MyMethod_Result);
    }
</script>

<input id="test1" type="button" value="Test via server" name="test1" onclick ="CallMyMethod()" />
<input id="test2" type="button" value="Test Direct" name="test2" onclick ="window.open('http:\/\/www.bing.com/maps/?v='+'6.2&mkt=fr-FR')" />
   
    
    </form>
</body>
</html>


Imports System.Web.Services
Partial Public Class _Default
    Inherits System.Web.UI.Page

    <WebMethod()> _
    <Script.Services.ScriptMethod()> _
    Public Shared Function MyMethod(ByVal name As String) As String
        Return "6.2&mkt=fr-FR"
    End Function
End Class

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Hello Ivicarp,

The first one can be considered as unsolicited and the other one solicited by the user.

Regards.
The new browser generation want to protect the user
Avatar of Ivicarp
Ivicarp

ASKER

leakim971 thanks for your reply. What makes window.open to behave differently? I use all five major browser with the latest versions. Still no explanation of why the same function allows in all of the browser to open up a tab or new window when url is passed as a string but not as a variable.
I checked also with ASP buttons thinking that perhaps the trip to the server (postback) was the culprit.
After testing more than a week, the conclusion I have is that if passing the url via a string it does not block, if you pass it via a variable it makes the blocker to work. WHY??


Avatar of Ivicarp

ASKER

I did more testing and I am changing my opinion, I believe now that it has to do with a postback issue.
If somebody could confirm this, I would hate to translate to a client function the preparation of the string for the URL for it not to work later on.
Refer to the following links. These will help.
http://msdn.microsoft.com/en-us/library/ms537632(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms536753(v=VS.85).aspx

Also, you can modify your code as below.
<script language="javascript" type="text/javascript">
    function MyMethod_Result(ResultString) {
        var urlmap = 'http:\/\/www.bing.com/maps/?v=' + ResultString;
        var result = window.open(urlmap);
        writeTo(result);
    }
    function CallMyMethod() {
        PageMethods.MyMethod("World", MyMethod_Result);
    }
    function writeTo(w) {
        w.document.write('Test');
     }
</script>

If the above solution does not work then you have no option but to create a custom popup. Use below given link, it has the code to create a custom popup. This site shows you the sample also.
http://www.dynamicdrive.com/dynamici...htmlwindow.htm
Avatar of Ivicarp

ASKER

Gagan thank you for your replies.
On the following line for some reason that I dont understand,  result ends with a null value even though urlmap has the proper value in it

var result = window.open(urlmap);
Please advice
Thanks
Avatar of Ivicarp

ASKER

Gagan:
The link provided has a typo, I tried to find the proper page but couldnt.
If you would be kind enogh, thanks.

http://www.dynamicdrive.com/dynamici...htmlwindow.htm
ASKER CERTIFIED SOLUTION
Avatar of Gagan_Jaura
Gagan_Jaura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ivicarp

ASKER

Gagan:

I had to convert my codebehind function to client javascript, get the database data required and load on hidden variables at onload, once the input button was pressed, then verify my checkboxes on aspx to see which hidden variables I had to use and prepare the string on the client side and finally use window.open. By doing all of these i managed to avoid  a postback and got the button to bypass the popup blockers on all major browser.
Thank you all. Gagan I am giving you the points. Your proposal open my eyes to find the solution.