Mr A
Mr A

Reputation: 6768

Problems with opening the link on another page

I am working on contact page where the client can enter the postcode which will take them to google maps for directions to the company, the problem which i am having is although the hyperlink is set to target_blank but still the window opens on the back hand instead of opening in front of the website page. I have no idea why it opens on the back hand and focus is on the current page instead of moving it to google map page

  <a href="#" target="_blank">
               <img alt="" src="/images/contactUs/directionbtn.png" onclick="return openDirections(1);"  /></a>
<script type="text/javascript">

function openDirections(NumVal) {

    if (NumVal == 1) {

        if (document.getElementById("<%=txtPostcode.ClientID%>").value == "") {
            alert("PostCode can not be blank");
            document.getElementById("<%=txtPostcode.ClientID%>").focus();
            return false;
        }
        else {

            var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
            var tempURL = document.getElementById("<%=txtPostcode.ClientID%>").value;
            if (regPostcode.test(tempURL) == false) {
                alert("Please Enter a Valid PostCode");
                document.getElementById("<%=txtPostcode.ClientID%>").focus();
                return false;
            }
            else {
                var url = 'http://maps.google.co.uk/maps?saddr={' + $('#<%=txtPostcode.ClientID%>').val() + '}&daddr=&daddr=646+Preston+Rd,+Clayton-le-Woods,+Chorley+PR6+7EH,+United+Kingdom&iwloc=1&dq=Tangent+Design';


                document.location = url;
                return true;
            }
        }
    }
    </script>

Upvotes: 1

Views: 231

Answers (4)

radbyx
radbyx

Reputation: 9660

I think this is what you are looking for:

var x = window.open(URL, 'name', '...');
x.focus();

Upvotes: 0

Fredrik
Fredrik

Reputation: 2016

Try window.open(url); instead of of document.location = url;

Upvotes: 1

Zach Green
Zach Green

Reputation: 3460

Each browser can be configured to handle how new pages are opened. Take a look at the preferences in the browser you are using, and see if that is the behavior that you currently have configured.

Upvotes: 1

Matthew Cox
Matthew Cox

Reputation: 13672

All I see you doing is setting the url of the current window. Maybe try using something like this to open a new window instead.

window.open('url to open',)

in place of document.location = url

Upvotes: 1

Related Questions