trobbins26
trobbins26

Reputation: 219

Form Button open a new page onclick

I'm calling the script below on a button onclick, where .value is a URL dependent on a drop down section. I want the resulting URL to open in a new window ('_blank') when the button is clicked. How can I modify this code to do that? Any help is appreciated.

function openResultsPage() {
    if(document.getElementById('selectedRace').value){
                            window.location.href = document.getElementById('selectedRace').value;
                        }   
                    }

Upvotes: 1

Views: 4331

Answers (3)

Anoop
Anoop

Reputation: 23208

You should use window.open to open a new window

NOTE: I am using height and width in third parameter to make sure that window.open open new window instead of new tab

function openResultsPage() {
    if(document.getElementById('selectedRace').value) {
        window.open(document.getElementById('selectedRace').value,'','width=700, height=500');
    }  
}

Upvotes: 1

abbotto
abbotto

Reputation: 4339

You can try this:

function openResultsPage() {
  if(document.getElementById('selectedRace').value){
    var href = document.getElementById('selectedRace').value
    window.open(href,'_blank').value;
  }   
}

Upvotes: 2

Amit
Amit

Reputation: 22076

You can try this.

<head>
<base target="_blank">
</head> 

Upvotes: 0

Related Questions