Search code examples
javascriptvb.netquirks-mode

window.open in quirks mode also changes subsequent page to quirks mode


I'm using window.open to open a new window from code behind to a page which runs in quirks mode. However, when I click this button to go to this second page, it changes the first page mode as well.

This is the piece of code that I wrote:

Dim url1 As String = "MYURL.html"
Response.Write("<script>")
Response.Write("newwindow=window.open('" + url1 + "','_blank');")
Response.Write("newwindow.focus()")
Response.Write("</script>")

Is there a way to fix this problem?

Note: my target page really should run in quirks mode, so this is not a part that I can ignore, and I have to do this with a button that runs at the server, so I cannot achieve the same effect with an anchor tag which has its target set to _blank.


Solution

  • Finally I found a way but this is a bit of a work around however it will do the job for now

    Dim url1 As String = "MYURL.html"
    Response.Write("<script>")
    Response.Write("newwindow=window.open('" + url1 + "','_blank');")
    Response.Write("newwindow.focus()")
    Response.Write("window.open('" + HttpContext.Current.Request.Url.AbsoluteUri + "','_self')")
    Response.Write("</script>")
    

    the line below of the code would get the same url and open it in the same tab (like what location.reload(); would do but I couldn't use it in this situation so I came up with this substitute) and as the first page has a meta tag to open the page in ie edge everything would seem fine.

    Response.Write("window.open('" + HttpContext.Current.Request.Url.AbsoluteUri + "','_self')")
    

    Edit: if the pop-up blocker, blocks the site still the first page would go to quirk mode.. but with the code below it would alert the user to fix the problem and if the pop up blocker was disable it shows the second page

    Dim url1 As String = "MYURL.html"    
    Response.Write("<script>")
    Response.Write("newwindow=window.open('" + url1 + "','_blank');")
    Response.Write("if(!newwindow || newwindow.closed || typeof newwindow.closed=='undefined'){")
    Response.Write("alert('please disable pop-up blocker for this site');")
    Response.Write("}")
    Response.Write("else{")
    Response.Write("newwindow.focus();")
    Response.Write("}")
    Response.Write("window.open('" + HttpContext.Current.Request.Url.AbsoluteUri + "','_self')")
    Response.Write("</script>")
    

    Edit 2: I finally found out what was the problem...actually response.write was the reason that my first page go to the quirks mode and to fix it I used Page.RegisterClientScriptBlock like bellow to open my window and everything worked like it should

    Page.RegisterClientScriptBlock("", "<script>window.open('" & MYURL & "', '_blank', 'toolbar=no');</script>")